Working with variables
Posted by David on March 13th, 2007 filed in JavaScriptA variable is a named placeholder for a value. You can use the var keyword to construct an expression that first declares a variable and then (optionally) initializes its value.
How to declare a variable:
var myName;
How to initialize a value:
var myName = “David Yin”
Technically, you can declare a variable in JavaScript without using the var keyword, like so :
myName = “David Yin”
However, using the var keyword to declare all your variables is a good practice.
The scope is very important. A variable is valid only when it’s in scope. When a variable is in scope, it’s been declared between the same curly brace boundaries as the statement that’s trying to access it.
If you define a variable inside a function, this variable works only in this function.