Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JS function examples

小云云
Release: 2018-02-24 14:29:34
Original
1905 people have browsed it

A function is a repeatable block of code that is event-driven or executed when it is called. JavaScript function syntax. A function is a block of code wrapped in curly braces. The keyword function:

is used before. function functionname(){

Here is the code to be executed

}

When this function is called, the code within the function will be executed

The function can be called directly when an event occurs (such as when the user clicks a button), and can be called at any position by JavaScript

JavaScript is case-sensitive, the keyword function must be lowercase, and must be the same as function Call a function with the same case name

Calling a function with parameters

When you call a function, you can pass it values, which are called parameters.

These parameters can be used in functions.

You can send as many parameters as you like, separated by commas (,).

myFunction(argument1,argument2)
Copy after login

When you declare a function, please declare the parameters as variables.

function myFunction(var1,var2)
{
这里是要执行的代码
}
Copy after login

Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed.

The function is very flexible. You can call the function with different parameters, which will give different messages.

Function with return value

Sometimes we want to return the value to the place where it was called

This can be achieved by using the return statement

When using the return statement, the function will stop execution and return the specified value

return x;

return;

You can also do this when you just want to exit the function Using return, the return value is optional;

Local JavaScript variables

Variables declared inside JavaScript (using var) are local variables, so they can only be used in functions Access it internally. (The scope of this variable is local)

You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.

As soon as the function completes running, local variables will be deleted

Global JavaScript variables

Variables declared outside the function are global variables, on the web page All scripts and functions have access to it.

The lifetime of JavaScript variables

The lifetime of JavaScript variables starts from the time they are declared.

Local variables will be deleted after the function is run

Global variables will also be deleted after the page is closed

Assign values ​​to undeclared JavaScript variables

If a value is assigned to a variable that has not been declared, the variable will automatically be declared as a global variable

Related recommendations:

JS function parameters passed by value

Detailed explanation of JS function throttling and anti-shaking examples

Detailed analysis of JS function debouncing and throttling

The above is the detailed content of Detailed explanation of JS function examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template