简体   繁体   中英

Is there any difference/advantage of using var before a variable name in javascript?

I am learning javascript . And I noted that the instructor was making variables in two ways . first was (for example):

var name ="Any string here";

and second method was simply writing the variable name without writing var first :

name="Any string here";

And the result was same . So , is there any difference between these two ?. Which one is better to use ?

离开了var使其成为一个全局变量,所以如果你有name多个地方的代码,他们会互相覆盖。

  • First of all, it is a bad practice to not write var before variable declaration.
  • Second problem is a global declaration of variable without var .
  • Third, in strict mode using variable without declaration will cause Exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM