JavaScript: Variable

By Xah Lee. Date: . Last updated: .

Variable Declaration

Use var name; to declare a variable.

(Note: For JavaScript ES2015, you should use let instead. The syntax is the same as var.)

〔►see JavaScript: let Declaration

// declare variable
var x;
var y;

Multiple variables can be declared on the same line, separated by comma.

For example, var name1, name2, name3 …;

// declare multiple variables
var a, b, c;

Multiple variables can be declared and assigned at the same time.

// declare variable and assign
var x = 4;
// declare variable and assign multiple variables
var a = 1, b = 2, c = 3;
// declare multiple variables, some with value
var a, b = 2, c;

console.log(a); // undefined
console.log(b); // 2
console.log(c); // undefined

When a variable is declared but not assigned, the default value is undefined:

var x;
console.log(x); // prints 「undefined」

When a variable is not declared nor assigned, and you try to access it, you get ReferenceError.

var x;
console.log(x); // prints 「undefined」
console.log(y); // ReferenceError: y is not defined

You should ALWAYS declare variables. It is harmless if you declared a variable redundantly multiple times.

var x;
console.log(x);                 // undefined
var x;
console.log(x);                 // undefined

var y = 3;
console.log(y);                 // 3
var y;
console.log(y);                 // 3

Variable Assignment

Variable assignment returns a value.

// variable assignment returns a value
var xx;
console.log(xx = 3);            // 3

So, variable assignment can be chained.

var xx;
var yy;

console.log(xx = yy = 3);       // 3
console.log(xx);                // 3
console.log(yy);                // 3

Local Variable

Variable declared inside a function is local to the function. Meaning, the variable exist only inside the function.

// example of local variable

var n = 2; // this is global variable

function f ()  {
    var z = 3; // this is local variable. it exists only inside function f

// ReferenceError: z is not defined

Local variables are independent of global ones.

// Local variables are independent of global ones.

var n = 2; // global variable

function f ()  {
    var n = 3; // this is local variable
    console.log(n); // 3


Variable inside a curly bracket does not create a scope.

var n = 2;

function f ()  {
    var n = 3;
        var n = 4;

f(); // prints 4

When a variable is not declared but used, JavaScript will search the variable in outer scopes, until it reaches the global space.

JavaScript Variable Topic

  1. JavaScript: Variable
  2. JavaScript: Variable/Function Declaration Order, Name Hoisting
  3. JavaScript Variable Scope: Function Level vs Block Level
  4. JavaScript: let Declaration
  5. JavaScript: const Declaration

JavaScript Basics

  1. How to Run JavaScript
  2. JavaScript: How to Use Browser's JS Console
  3. JavaScript Tutorial by Example
  4. JavaScript: Data Types
  5. JavaScript: true, false
  6. JavaScript: Operators
  7. JavaScript: Variable
  8. JavaScript: Branch Control: if then else, switch
  9. JavaScript: for while do Loop
  10. JavaScript: Array Basics
  11. JavaScript: Object Basics
  12. JavaScript: RegExp Tutorial
  13. JavaScript: Throw Try Catch Finally
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.