Home  >  Article  >  Web Front-end  >  js global and local variables and operators knowledge sharing

js global and local variables and operators knowledge sharing

小云云
小云云Original
2018-03-07 11:17:541460browse

1. This article mainly shares with you the knowledge of js global and local variables and operators. First, we will explain the relevant knowledge of local variables and global variables to you, hoping to help you.

2. Local variables and global variables
Local variables: Variables declared inside a js function are local variables, so they can only be accessed inside the function, that is, the scope of the variable is local. So you can use local variables with the same name in different functions.

  1. Use variables defined by var inside the function

  2. Parameter variables of the function

Global variables : A variable declared outside a function is a global variable and can be accessed by all scripts and functions on the web page.

  1. Variables defined outside the function body

  2. Variables defined inside the function body without var declaration

Priority: local variables are higher than global variables with the same name, parameter variables are higher than global variables with the same name, and local variables are higher than parameter variables with the same name.
When passing values ​​to parameters in functions, the values ​​must be enclosed in single quotes. It is strongly recommended to use single quotes.
Scope chain: The inner function can access the local variables of the outer function, but the outer function cannot access the local variables of the inner function.
Life cycle of variables: All variables start when they are declared. Local variables will be deleted after the function is run. Global variables will be deleted after the page is closed.
If a value is assigned to a variable that has not been declared, the variable will be automatically declared as a global variable.
3. Operator
+: Can be used to connect strings. Add two numbers and return the sum of the numbers. If numbers and characters are added, a string is returned. If you add a number to a string, the result will be a string.
==: Only compare values ​​
===: Absolutely equal, compare values ​​and types
! ==: Not absolutely equal (one of the value and type does not want to wait, or both are not equal)
Priority of logical operators:! , &&, ||
Ternary operator: (condition)? true-doing: else-doing
Loop: break ends the entire loop (used in loops and switch statements), continue ends this loop (only used in loops)
4. Type conversion
Number ( ) Convert to number String() Convert to string Boolean() Convert to Boolean value
The data type of NaN is number, array (Array), date (Date), and null type are all objects, and undefined variables are undefined .

2. Local variables and global variables
Local variables: The variables declared inside the js function are local variables, so they can only be accessed inside the function, that is, the scope of the variable is local. So you can use local variables with the same name in different functions.

  1. Use variables defined by var inside the function

  2. Parameter variables of the function

Global variables : A variable declared outside a function is a global variable and can be accessed by all scripts and functions on the web page.

  1. Variables defined outside the function body

  2. Variables defined inside the function body without var declaration

Priority: local variables are higher than global variables with the same name, parameter variables are higher than global variables with the same name, and local variables are higher than parameter variables with the same name.
When passing values ​​to parameters in functions, the values ​​must be enclosed in single quotes. It is strongly recommended to use single quotes.
Scope chain: The inner function can access the local variables of the outer function, but the outer function cannot access the local variables of the inner function.
Life cycle of variables: All variables start when they are declared. Local variables will be deleted after the function is run. Global variables will be deleted after the page is closed.
If a value is assigned to a variable that has not been declared, the variable will be automatically declared as a global variable.
3. Operator
+: Can be used to connect strings. Add two numbers and return the sum of the numbers. If numbers and characters are added, a string is returned. If you add a number to a string, the result will be a string.
==: Only compare values ​​
===: Absolutely equal, compare values ​​and types
! ==: Not absolutely equal (one of the value and type does not want to wait, or both are not equal)
Priority of logical operators:! , &&, ||
Ternary operator: (condition)? true-doing: else-doing
Loop: break ends the entire loop (used in loops and switch statements), continue ends this loop (only used in loops)
4. Type conversion
Number ( ) Convert to number String() Convert to string Boolean() Convert to Boolean value
The data type of NaN is number, array (Array), date (Date), and null type are all objects, and undefined variables are undefined .

Related recommendations:

PHP global and local variables

About the different distinctions between global variables global and $GLOBALS in PHP - WORSHIP Asaph

php global variables What are super global variables

The above is the detailed content of js global and local variables and operators knowledge sharing. 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