javascript variable declaration instance analysis

  • 2020-06-01 08:22:13
  • OfStack

The example in this article shows you how to declare the javascript variable. Share with you for your reference. The specific analysis is as follows:

A variable in js should be declared before it is used. Variables are declared using the keyword var.

If you do not specify an initial value for a variable in an var declaration statement, the variable value is undefined.
Instead of specifying the variable type when you declare a variable, the js variable can be any data type.

It is legal and harmless to repeatedly declare variables using the var statement. If repeated declarations with initializers are no different from simple assignment statements.

If you try to read an undeclared variable, js will report an error. In ECMAScript5 strict mode, assigning a value to an undeclared variable will also result in an error. Historically, however, in non-strict mode, if you assign a value to an undeclared variable, js actually creates a property of the same name for the global object, and it looks like it works like a properly declared global variable. This means you can get away with not declaring global variables, but it's a bad habit that can cause a lot of bug. It's best to always declare variables using var.

In a function, a local variable of the same name overrides a global variable.
Although global scope code can be written without the var statement, you must use the var statement when declaring local variables, as shown in the following code:


scope = "global";
function foo(){
  scope="local"
  //fk ! We just modified the global variable!! 
}

In programming languages like C, each piece of code in curly braces has its own scope, and variables are not visible outside of the declared code segment. This is called a block-level scope (block scope). Instead of block-level scopes, js USES function scopes (function scope): variables are defined in the body of the function in which they are declared, and in any function in which that body is nested (whether inline or nested?).
The function scope of js means that all variables declared in a function are always visible in the body of the function, which means that variables can even be used before they are declared. This feature of js is informally known as declaration prepositioning (hoisting), where all variables declared in the js function (but not assigned) are "prepositioned" to the top of the function body.


var scope = "global";
function f(){
  console.log(scope);
  // The output "undefined" Rather than "global"
  var scope = "local";
  // Variables are initially assigned here, but variables are defined everywhere in the body of the function 
  console.log(scope);
  // The output "local"

The above code is equivalent to:


function f(){
  var scope;
  console.log(scope);
  scope = "local";
  console.log(scope);
  }

When you declare an js global variable, you actually define one property of the global object.
When a variable is declared with var, the property created is not configurable, that is, cannot be deleted with the delete operator. But when you don't use strict mode and assign a value to an undeclared variable, js will automatically create a global variable. Variables created in this way are normally configurable properties of the global object and can be deleted:


var x = 1; 
y = 2;
this.z = 3; // Same as above 
delete x; // return false , the variable cannot be deleted 
delete y; // return true , the variable is deleted 
delete this.z // Same as above 

I hope this article is helpful for you to design javascript program.


Related articles: