Variables


Variables in JavaScript are fundamental constructs used to store and manipulate data. They act as containers for data values and can hold various types of data such as numbers, strings, objects, arrays, and more. Understanding how to declare, initialize, and use variables is crucial for effective JavaScript programming.

Declaring Variables

In JavaScript, variables can be declared using three main keywords: var, let, and const. Each has its own scope and characteristics.

  1. var

    • var is the traditional way to declare variables. It is function-scoped, meaning the variable is accessible within the function it is declared in, or globally if declared outside any function. Variables declared with var can be re-declared and updated.

    Example:

    var name = "Alice"; console.log(name); // Output: Alice var name = "Bob"; // Re-declaration is allowed console.log(name); // Output: Bob
  2. let

    • let is a more modern way to declare variables. It is block-scoped, meaning the variable is only accessible within the block (enclosed by {}) it is declared in. Variables declared with let can be updated but not re-declared within the same scope.

    Example:

    let age = 25; console.log(age); // Output: 25 age = 30; // Updating the variable console.log(age); // Output: 30 // let age = 35; // Error: Identifier 'age' has already been declared
  3. const

    • const is used to declare constants—variables that cannot be reassigned after their initial value is set. Like let, const is block-scoped. It is used for values that should remain constant throughout the code.

    Example:

    const PI = 3.14159; console.log(PI); // Output: 3.14159 // PI = 3.14; // Error: Assignment to constant variable

Variable Types

JavaScript variables can hold different types of data, including:

  1. Primitive Data Types

    • Number: Represents numeric values (both integer and floating-point).
      let num = 42;
    • String: Represents sequences of characters, enclosed in single or double quotes.
      let greeting = "Hello, World!";
    • Boolean: Represents true or false values.
      let isActive = true;
    • Undefined: Represents a variable that has been declared but not yet assigned a value.
      let result; console.log(result); // Output: undefined
    • Null: Represents an intentional absence of value.
      let data = null;
    • Symbol: Represents a unique and immutable value often used as object property keys.
      let uniqueId = Symbol('id');
  2. Complex Data Types

    • Object: Represents a collection of key-value pairs. Useful for grouping related data.
      let person = { name: "Alice", age: 30 };
    • Array: Represents a list-like collection of values.
      let fruits = ["Apple", "Banana", "Cherry"];
    • Function: Represents a block of code that can be executed when called.
      function greet() { return "Hello!"; }

Variable Scope

Scope refers to the accessibility of variables in different parts of the code.

  1. Global Scope

    • Variables declared outside any function or block are globally scoped and accessible from anywhere in the code.
  2. Function Scope

    • Variables declared within a function using var are function-scoped and can only be accessed within that function.
  3. Block Scope

    • Variables declared within a block (using let or const) are block-scoped and can only be accessed within that block.

    Example:

    function example() { var x = 10; // Function-scoped let y = 20; // Block-scoped if (true) { var z = 30; // Function-scoped let w = 40; // Block-scoped console.log(x); // Output: 10 console.log(y); // Output: 20 console.log(z); // Output: 30 console.log(w); // Output: 40 } console.log(x); // Output: 10 console.log(y); // Output: 20 console.log(z); // Output: 30 // console.log(w); // Error: w is not defined }

Variable Hoisting

In JavaScript, variable declarations using var are "hoisted" to the top of their scope, meaning they are processed before any code is executed. However, their initial values are not hoisted, only the declaration itself.

Example:

console.log(name); // Output: undefined var name = "Alice"; console.log(name); // Output: Alice

Variables declared with let and const are not hoisted in the same way and will throw an error if accessed before their declaration.