• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keyword var

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, what does it mean to put a var before your variable? How come it also works without it? Thanks!
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To declare (create) a variable, you use the JavaScript statement var (which stands for variable) followed by the name you want to assign the variable.

like
var Flag // Flag is name of variable..
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhmmm... Yeah, I'm aware of that... But I'm sure I've seen scripts that didn't use that statement...
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just now i create a simple program on javascript without using this statement. its not showing me the value of variable and then i use "var" it's running fine.

it's mean that variable cannot be decaler without use of "var" keyword so how it can works..?

i also want to know if there is a possiblity ?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JavaScript you do not have to declare a variabe. If you assign to a variable that has not been declared, it is automaticaloly declared at a global level.

So if you do not use var it is global. If you are expecting that i to be local for your for loop, declare it.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic