JavaScript Variable Types

When working with JavaScript, it’s important to know how to declare and use variables. Different keywords (var, let, const, global, and undefined) affect how variables behave in your code. Let’s explore each one.

1. var

  • Declaration: var variableName;
  • Example:
    function exampleFunction() {
    var x = 10;
    var x = 20; // Redeclaration is allowed within the same scope
    console.log(x); // Outputs 20
    }
  • Always Global: Yes, if declared outside a function.
  • Redeclare: Allows redeclaration within the same scope.
  • Reassign: Allows reassignment after declaration.
  • Scope: Function.
  • Hoisted: Yes, moves to the top of the function scope.
  • Bind this: Binds to the global object. this.x refers to the global scope.

Explanation: The var keyword is function-scoped, meaning it exists within a function. It can be redeclared and reassigned. Variables declared with var are hoisted to the top of the function, and they also bind to the global object if declared outside any function.

2. let

  • Declaration: let variableName;
  • Example:
    if (true) {
    let y = 'Hello';
    // let y = 'World'; // Error: Redeclaration not allowed
    y = 'World'; // Reassignment is allowed
    console.log(y); // Outputs 'World'
    }
  • Always Global: No.
  • Redeclare: Does not allow redeclaration within the same scope.
  • Reassign: Allows reassignment.
  • Scope: Block.
  • Hoisted: No, not hoisted.
  • Bind this: Does not bind to this.

Explanation: The let keyword is block-scoped, meaning it only exists within the block where it is defined. It prevents redeclaration within the same block but allows reassignment. Unlike var, let is not hoisted, so you cannot use it before it is declared.

3. const

  • Declaration: const variableName;
  • Example:
    const PI = 3.14;
    // const PI = 3.14159; // Error: Redeclaration not allowed
    // PI = 3.14159; // Error: Reassignment not allowed
    console.log(PI); // Outputs 3.14
  • Always Global: No.
  • Redeclare: Does not allow redeclaration.
  • Reassign: Does not allow reassignment.
  • Scope: Block.
  • Hoisted: No, not hoisted.
  • Bind this: Does not bind to this.

Explanation: The const keyword is also block-scoped and does not allow you to redeclare or reassign the variable. It is mostly used for constants, values that do not change.

4. global

  • Declaration: variableName = value;
  • Example:
    globalVar = true;
    globalVar = false; // Reassignment is allowed
    console.log(globalVar); // Outputs false
  • Always Global: Yes.
  • Redeclare: Does not apply.
  • Reassign: Allows reassignment.
  • Scope: Global.
  • Hoisted: No.
  • Bind this: Does not bind to this.

Explanation: If you declare a variable without using var, let, or const, it becomes a global variable. These variables can be reassigned and are accessible throughout your code.

5. undefined

  • Declaration: let z;
  • Example:
    function exampleFunction() {
    let z;
    // let z; // Error: Redeclaration not allowed
    console.log(z); // Outputs undefined
    }
  • Always Global: No.
  • Redeclare: Does not allow redeclaration within the same scope.
  • Reassign: Not applicable; it is undefined by default.
  • Scope: Block.
  • Hoisted: No.
  • Bind this: Does not bind to this.

Explanation: Variables declared with let or const are initialized with undefined by default. They are block-scoped, and redeclaration within the same scope is not allowed.

Featurevarletconstglobalundefined
Declarationvar variableName;let variableName;const variableName;variableName = value;let z;
ScopeFunctionBlockBlockGlobalBlock
Always GlobalYes, if declared outside a functionNoNoYesNo
RedeclarationAllowedNot allowedNot allowedDoes not applyNot allowed
ReassignmentAllowedAllowedNot allowedAllowedNot applicable
HoistedYesNoNoNoNo
Bind thisBinds to the global object (this.x)Does not bindDoes not bindDoes not bind (this.globalVar)Does not bind (this.z)
Examplevar x = 10; var x = 20;let y = 'Hello'; y = 'World';const PI = 3.14;globalVar = true; globalVar = false;let z; console.log(z);
Scroll to Top