In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the JavaScript variable promotion method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
JavaScript variable promotion
The Hoisting JavaScript will declare the default behavior of moving to the top.
The JavaScript declaration is promoted, and in JavaScript, variables can be declared after use. To put it another way; variables can be used before declaration. Example 1 shows the same result as example 2:
Example 1:
Js
X = 5; / / assign x 5 elem = document.getElementById ("demo"); / / find an element Elm [XSS _ clean] = x; / / display x var x; / / declare x within the element
Example 2:
! DOCTYPE html > js
Var x; / / declare x x = 5; / / assign 5 to x elem = document.getElementById ("demo"); Elm [XSS _ clean] = x
To understand this, you must understand the term "declaration promotion (English: Hoisting)". Is the default behavior of JavaScript to move all declarations to the top of the current scope (to the top of the current script or function).
However, this default behavior has been changed in ES6, and the variables and const declaration constants declared by ES6's let will not be promoted!
JavaScript initialization does not promote
JavaScript only promotes the declaration, not initializes it. Example 1 and example 2 are different results:
Example 1:
Js
Var x = 5; / / initialize x var y = 7; / initialize y elem = document.getElementById ("demo"); / / find an element Elm [XSS _ clean] = x + "" + y; / / display x and y
Example 2:
Js
Var x = 5; / initialize x elem = document.getElementById ("demo"); / / find an element Elm [XSS _ clean] = x + "" + y; / / display x and y var y = 7; / / initialize y
Declare your variables at the top!
For many developers, variable promotion (Hoisting) is an unknown or neglected JavaScript behavior. If the developer does not understand promotion, the program may contain errors (Error). To avoid errors, always declare all variables at the beginning of each scope. Because this is the way JavaScript interprets the code, it is always a good rule.
This is the end of the content of "what is the JavaScript variable promotion method". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.