Home  >  Article  >  Web Front-end  >  A brief discussion on Javascript variable scope issues_javascript skills

A brief discussion on Javascript variable scope issues_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:26:171099browse

Variable scope problem in Js:

1. There is no block-level scope. The variable scope in JS is not bounded by {}, unlike C/C/Java.

For example:

Copy code The code is as follows:

if(true){
var name = "qqyumidi";
}
                                                                       alert(name); // Result: qqyumidi

JS will add the variables defined in if to the current execution environment. Especially when using for loops, you need to pay attention to the differences with other languages.

Copy code The code is as follows:
for(var i=0; i<10; i ){
;
}

alert(i); // Result: 10

This is just my personal understanding. If there are any mistakes, please let me know.

Statement:
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