Home  >  Article  >  Web Front-end  >  Detailed example of how to distinguish whether a variable is defined in JavaScript with or without var

Detailed example of how to distinguish whether a variable is defined in JavaScript with or without var

伊谢尔伦
伊谢尔伦Original
2017-07-18 10:11:151279browse

Let’s summarize the difference between using the keyword var or not when defining variables.

# Inside the function, variables declared with var and without var are different. Those declared with var are local variables, and those without var are declared global variables, so you can use this to expose interface stuff to the outside world.

It is legal and harmless to use var statements to repeat declaration statements. If the statement is repeated with an assignment, it is no different from a normal assignment statement. If you try to read an undeclared variable, JS will report an error.

Within the function scope of JavaScript, the declared variables or internal functions are visible in the function body. Meaning, the function may be available before it is defined. There are two ways to define a function, one is a function definition expression, and the other is a function declaration statement.

Function declaration statements are "advanced" to the top of the external script or external function scope, so a function declared in this way can be called by code that appears before it is defined. In function definition expressions, the declaration of variables is advanced, but the assignment to variables is not advanced. Therefore, functions defined in expressions cannot be called before the function is defined.

1. In the function scope, variables defined with var are local variables, and variables defined without var become global variables.
Use var definition:


var a = 'hello World';
function bb(){
 var a = 'hello Bill';
 console.log(a);  
}
bb()      //'hello Bill'
console.log(a);  //'hello world'

Do not use var definition:


var a = 'hello World';
function bb(){
 a = 'hello Bill';
 console.log(a);  
}
bb()      //'hello Bill'
console.log(a);  //'hello Bill'

2. In the global scope Below, variables defined using var cannot be deleted, and variables defined without var can be deleted. This means that implicit global variables are not strictly speaking real variables, but attributes of the global object, because attributes can be deleted through delete, and variables Can't.

3. Using var to define a variable will also promote the variable declaration, that is,
Use var definition:


function hh(){
 console.log(a);
 var a = 'hello world';
}
hh()      //undefined

Do not use var definition:


function hh(){
 console.log(a);
 a = 'hello world';
}
hh()      //'a is not defined'

This is the declaration of variables defined using var in advance.

4. In ES5's 'use strict' mode, if a variable is not defined using var, an error will be reported.

The above is the detailed content of Detailed example of how to distinguish whether a variable is defined in JavaScript with or without var. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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