Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Does javascript have block-level scope?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces whether javascript has block-level scope, the article is very detailed, has a certain reference value, interested friends must read it!

In JavaScript, there is no concept of block-level scope. Variables defined in block-level statements are actually created in the containing function, not in the statement, and the variable declaration can be placed at the top of the function body instead of close to where the variable is used.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

Does javascript have a block-level scope?

Javascript has no concept of block-level scope. This means that variables defined in block-level statements are actually created in the containing function rather than in the statement.

Code snippet 1:

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

What will be output?

Answer: undefined local

Code snippet 2:

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

What will it output?

Answer: undefined local

Code snippet 3:

Var scope= "global"; function f () {console.log (scope);} f ()

What will it output?

Answer: global

Through the above three examples, the following explains the sentence "JavaScript has no block-level scope, it has a function scope".

In JavaScript, because of the nature of function scope, code snippet 1 and code snippet 2 are equivalent, and local variables are defined in the whole function body.

That is, the local variable scope of code snippet 1 in the function body overrides the global variable of the same name, and only when the program executes the var statement will the local variable scope be truly

Assign a value. Therefore, the above process is equivalent to "advancing" the variable declaration within the function to the top of the function body, while leaving the variable initialization in its original place: code snippet 2.

In a programming language with block-level scope, it is generally good programming to keep the code that declares and uses variables as close to each other as possible in a narrow scope.

Habit. Because JavaScript has no block-level scope, some programmers deliberately place variable declarations at the top of the function body, rather than near them where variables are used. This kind of practice

Their source code clearly reflects the variable scope of knowledge.

The above is all the content of the article "does javascript have block scope?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report