This is the 14th day of my participation in the August Text Challenge.More challenges in August

JavaScript variables are containers that store data values. The scope of JavaScript variables can be divided into global scope, local scope and block-level scope understanding. All JavaScript variables must be identified with a unique name.

These unique names are called identifiers.

Identifiers can be short names (such as X and Y) or more descriptive names (age, sum, totalVolume).

The general rule for constructing variable names (unique identifiers) is:

  • The name can contain letters, digits, underscores (_), and dollar signs
  • Names must begin with a letter
  • Names can also start with $and _ (but we won’t do that in this tutorial)
  • Names are case-sensitive (y and y are different variables)
  • Reserved words (such as JavaScript keywords) cannot be used as variable names

Tip: JavaScript identifiers are case sensitive.

1. Global variables

Global variable: any variable declared using var anywhere else, except functions, is a global variable and can be used anywhere on the page.

Global variables, if the page is not closed, then the memory occupied by the variable will not be released, it will take up space, memory consumption.

var lut = 520; console.log(lut); / / 520Copy the code

2. Local variables

Local variable: a variable defined inside a function. It is a local variable and cannot be used outside.

function lut() { var luzp = 410; } lut(); console.log(luzp); // Luzp is not definedCopy the code

Block-level scope

Block-level scope: a pair of curly braces can be used as a block. Variables defined in this area can only be used in this area, but variables defined in this block-level scope can also be used outside of JS. Js has no block-level scope, except for functions.

{ var lut = 520; console.log(lut); // 520 } console.log(lut); if(true){ var lut = 1314; } console.log(lut); for(var i = 0; i < 5; i++){ var lut = 520; } console.log(lut); // 520 var i = 0; while (i < 5){ var lut = 1314; i++; } console.log(lut); / / 1314Copy the code

Implicit global variables

Implicit global variables: Declared variables without var are called implicit global variables.

function lut() { date = 410; // is an implicit global variable} lut(); console.log(date); // the 410 function can be accessed at the end of the runCopy the code

Note:

1. Global variables cannot be deleted. Implicit global variables can be deleted

2, the definition of variables using var is not deleted, no var can be deleted

3. Always use var when defining variables

var luzp = 27; lut = 23; delete luzp; // delete luzp; Console. log(typeof luzp); // Number cannot be deleted console.log(luzp+10); // 37 console.log(typeof lut); // undefined can be deletedCopy the code