Home > Web Front-end > JS Tutorial > What does js variable promotion mean? Introduction to the use of js variable promotion (example)

What does js variable promotion mean? Introduction to the use of js variable promotion (example)

不言
Release: 2018-09-15 16:27:10
Original
5405 people have browsed it

The content of this article is about what does js variable promotion mean? An introduction to the use of js variable promotion (examples) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction

Variable hoisting Hoisting is people’s understanding of the way JavaScript execution context works, and it is not an official change

Literally understood, variable promotion means that the declarations of variables and functions will be moved to the front of the scope in the physical layer. But this understanding is not accurate, the effect is the same, but the actual implementation is that the declaration of JavaScript variables and functions will be put into memory during the compilation phase

This means that the user is formally declaring a function or variable We have been able to use it before

Function promotion

In JavaScript, we can use it before declaring a function, everyone should I have experienced it all, like this:

test();

function test() {
    // do something
}
Copy after login

Under normal usage, you should declare the function before calling it, but this method can still run because JavaScript automatically stores the function declaration in memory in advance. The reason is that it looks like JavaScript automatically promotes the function declaration to the front

Variable promotion

For variables, JavaScript uses a similar method, but one thing to note The thing is, for variable promotion, JavaScript will only promote the variable declaration, but will not promote the initialization. If it is used before the variable is initialized, you will get undefined

// undefined
console.log(a);
// ReferenceError: b is not defined
console.log(b);
var a = 10;
Copy after login
// undefined
console.log(num);
num = 6;
// 6
console.log(num);
num += 7;
// 13
console.log(num);
var num;
Copy after login
// undefined
console.log(num);
num = 1;
// 1
console.log(num);
var num = 2;
// 2
console.log(num);
Copy after login

Note here, JavaScript# The variable promotion of ## is for var, while let and const do not have the feature of variable promotion

// ReferenceError: a is not defined
console.log(a);
let a = 10;
Copy after login

A more complicated example

var a = 100;
function fn() {
    // undefined
    console.log(a);
    var a = 200;
    // 200
    console.log(a);
}
fn();
// 100
console.log(a);
var a;
// 100
console.log(a);
// 300
var a = 300;
console.log(a);
Copy after login
Related recommendations:

Detailed explanation of js variable promotion

In-depth understanding of scope and variable hoisting in JS

The above is the detailed content of What does js variable promotion mean? Introduction to the use of js variable promotion (example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template