JS: Variable

By Xah Lee. Date: . Last updated: .

Variable Declaration

var name; → declares a variable.

// 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
};

console.log(z);
// 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
}

f();

Variable inside a curly bracket does not create a scope.

var n = 2;

function f ()  {
    var n = 3;
    {
        var n = 4;
    }
    console.log(n);
};

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. JS: let Declaration
  2. JS: const Declaration
  3. JS: Variable
  4. JS: var/function Order
  5. JS: var Name Scope

JavaScript Basics

  1. How to Run JavaScript
  2. JS: How to Use Browser Console
  3. JavaScript Basics
  4. JS: Data Types
  5. JS: true, false
  6. JS: Operators
  7. JS: Variable
  8. JS: Branch Control: if then else, switch
  9. JS: for while do Loop
  10. JS: Array Basics
  11. JS: Object Basics
  12. JS: RegExp Tutorial
  13. JS: Throw Try Catch Finally
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon