Get the App
SLTechnology News&Howtos  ›  Development  › 

Will JavaScript initialization be improved?

Shulou Source: shulou.com Published: 2022-06-01 10:55:24 09月29日 Update

This article mainly introduces the relevant knowledge of JavaScript initialization will be improved, the content is detailed and easy to understand, the operation is simple and fast, it has certain reference value, I believe that after reading this JavaScript initialization will improve the article will have something to gain, let's take a look.

JavaScript initialization does not promote

Only declared variables in JavaScript will be promoted, not initialized ones.

The results of the following two examples are different:

Example 1

Var x = 5; / / initialize x

Var y = 7; / / initialize y

Elem = document.getElementById ("demo"); / / find the element

Elm [XSS _ clean] = x + "" + y; / / display x and y

Example 2

Var x = 5; / / initialize x

Elem = document.getElementById ("demo"); / / find the element

Elm [XSS _ clean] = x + "" + y; / / display x and y

Var y = 7; / / initialize y

Example 2's y outputs undefined because the variable declaration (vary) is promoted, but initialization (yinitialization 7) is not promoted, so the y variable is an undefined variable.

Example 2 looks like the following code:

Var x = 5; / / initialize x

Var y; / / statement y

Elem = document.getElementById ("demo"); / / find the element

Elm [XSS _ clean] = x + "" + y; / / display x and y

Y = 7; / / set y to 7

This is the end of the article on "will JavaScript initialization improve?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "will JavaScript initialization improve". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Examples variables elements knowledge content articles results same two code value only easy to operate articles easy to understand more look industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Linux macOS Shulou Technology