statement statement

1.var The
var statement is used to declare one or more variables. The syntax is as follows:
var name_1[=value_1][,...,name_n[=value_n]]
The keyword var is followed by a list of variables to be declared. Each variable of can have an initialization expression to specify its initial value, for example:
var i; //a simple variable
var j=1; //a variable with an initial value
var a,b; //Two variables
var x=1.2,y=x-1;z,s=x+y //Multiple variables
If the var statement appears in the function body, then it defines a local variable, and its scope is this function; if you use the var statement in the top-level code, it declares a global variable and is visible throughout the js program.
If the variable in the var statement does not specify an initialization expression, then the value of the variable is initially undefined.
2. The
function keyword function is used to define a function. Function definitions can be written in the form of statements. For example:
var f = function(x){ return x;} //Assign the expression to a variable
function f(x){ return x;} //Statement with variable name The syntax of a
function declaration statement is as follows:
function funcname( [arg1[,arg2[...,argn]]]) {
    statements
}
funcname is an identifier for the name of the function to be declared. Within parentheses after the function name is a list of parameters, separated by commas. When the function is called, these identifiers refer to the arguments passed into the function.
The function body is composed of js statements, and the number of statements is not limited, and they are enclosed in curly braces. The statement inside the function body is not executed when the function is defined, it is associated with the new function object to be executed when the function is called. The curly braces in the function statement are required.
An example of a function declaration:
function get_two_num(a,b){
    return a+b;
}
The function declaration statement usually appears at the top level of the js code, and can also be nested in other functions. But when nested, function declarations can only appear at the top of the nested function.
Although the function declaration statement and the function definition expression contain the same function name, they are still different. Both ways create a new function object, but the function name in the function declaration statement is a variable name, and the variable points to the function object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326344850&siteId=291194637