JavaScript variables (Variables)



 The purpose of variables is to store and read data from variables to facilitate calculations. Variables are like data containers.

For example, in the following example, xyz are all so-called variables:

var x = 1;
var y = 2;
var z = x + y;

It is a concept very similar to mathematical variables and expressions.

JavaScript Identifiers

The variable name you take, like the xyz in the above example, is called the identifier. You can use the identifier to refer to a variable.

The identifier must be unique and non-repetitive, and the new variable with the same name defined later will overwrite the previously defined variable.

The naming of JavaScript identifiers has certain rules and restrictions:

  1. Only letters az AZ (letters), digits 0-9 (digits), underscores _ (underscores) and money symbols $ (dollar signs) are allowed for the identification words.
  2. The beginning of the recognized word can only be a letter, underscore or money symbol.
  3. The recognition word is case sensitive, for example, the variable hello is not equal to the variable Hello.
  4. JavaScript reserved words (reserved words) cannot be used as variable names.

The letters allowed by JavaScript also include Unicode. Simply speaking Chinese can also be used as variable names, for example:

var Color ='blue';
alert(colour);

But generally, Chinese or other languages ​​are rarely used as variable names.

Declaring variables

JavaScript with varthe keyword to declare and establish a variable.

You can declare a variable name first:

var carName;

Assign another value to this variable:

carName = 'Ferrari';

Or you can specify a value while declaring the variable:

var carName = 'Ferrari';

The equal sign =is called the specified operator (assignment operator), is used to specify a value to a variable.

You can also use a comma- ,separated once more declare variables:

var car1 = 'Ferrari',
    car2 = 'Lamborghini',
    car3 = 'Porsche';
    
var fruit1, fruit2;

Variable scope (Variable Scope)

JavaScript variables have their scope of action, and code outside the scope of action cannot access the variable. The scope of JavaScript is the so-called function scope , that is, a new scope is created in a function.

According to function scope, we can divide variables into two types of scopes:

  1. Local variables
  2. Global variables

When JavaScript is looking for variables, it will follow the scope chain and look for it layer by layer until it finds the declared variable or can not find the variable (undefined).

function foo() {
    // Variables declared in the function, the scope of action (existence) is only in the function
    var carName = 'Ferrari';
    alert(carName); // Will show Ferrari
}

alert(carName); // An error will occur because the variable cannot be found

The variable declared outside the function is the so-called global variable. All JS code can access this variable:

// carName Is a global variable
var carName = 'Ferrari';
    
function foo() {
    alert(carName); // Will show Ferrari
}

alert(carName); // Will show Ferrari
Unlike other programming languages, JavaScript has a package level scope, only global or local, so a global variable can be accessed anywhere, even across different JS files. Therefore, it is generally avoided to use global variables to avoid contamination of the global namespace. Global variables with the same name in different files can easily be accidentally overwritten.

Camel Case

The variable naming style that JavaScript is used to is the so-called camel case.

Like firstName, lastName, carName, that is, the second letter is capitalized.

JavaScript reserved words (Reserved Words)

JavaScript reserved words are reserved for special purposes or specific syntax, so they cannot be used as variable names. JavaScript reserved words are:

break case catch const continue debugger default delete do else finally for function if in instanceof new return switch this throw try typeof var void while with

null true false

class enum export extends import super

implements interface let package private protected public static yield

Post a Comment

0 Comments