Tutorials

JavaScript ES6 Variables

Variables are the fundamental units of JavaScript programming, letting you store values to use throughout your code.

Key Takeaways

  • let and const are preferred over var due to their block-level scope and immutability respectively.
  • Variable names, called identifiers, must follow specific rules and should be descriptive.
  • Understand the scope of a variable to use it effectively.
  • Choose your variable declaration (var, let, const) based on the required scope and mutability.

Declaring a Variable

You need to declare a variable before using it.

Here's a straightforward example of a variable declaration using let:

let x = 2;

With let, x becomes an identifier that references the numeric value 2.

More on Identifiers

The chosen name for a variable is its identifier. Consider using descriptive identifiers to enhance code readability.

For instance:

let firstName = "Alice";

Rules for identifiers:

  • Must not be JavaScript reserved keywords like var, let, or function.
  • Can include letters, numbers, underscores, and dollar signs, but must not start with a number.

Examples:

let number2; // Valid declaration
let 2number; // Invalid declaration (starts with a number)

Variable Scope

Scope determines a variable's accessibility within different parts of your program.

Global Scope

Globally scoped variables are accessible from any part of your code.

let x = 2;
function add(){
    return x + 2;
}
// x is accessible within add because it's globally defined.

Local Scope

Locally scoped variables are accessible only within the function where they are declared.

function add(){
    let x = 2;
    return x + 2;
}
console.log(x); // Error: x is undefined outside add.

Block Scope

Variables declared with let and const have block scope.

function add(){
    let x = 2;
    {
        let x = 4;
    }
    return x + 2;
}
// Returns 4, the inner block's x does not affect the outer x.

Declaring Variables with var vs. let vs. const

Use the appropriate declaration based on scope and reassignability requirements:

var

Use var for function-scoped variables. However, since ES6, var is less common.

let

let provides block scope, which reduces the risk of bugs caused by variable redeclarations.

const

const ensures block scope and immutability after initial assignment.

const x = 4;
let y = 4;
y = 5; // Allowed
x = 5; // Error: x is constant and cannot be reassigned

Remember, const must be initialized upon declaration.

Conclusion

In modern JavaScript, let and const are preferred over var due to their predictable scoping behavior. Use let for variables that will change and const for variables that won't.

FAQ

Why should I use let instead of var?

let reduces the risk of errors due to its block-level scoping, unlike var, which is function-scoped and can cause unintended variable overwrites.

Can I reassign a variable declared with const?

No, const declarations create variables that cannot be reassigned. They must also be initialized at the time of declaration.

What happens if I declare a variable without initializing it?

Declaring a variable with let or var without initializing it will set it as undefined. With const, this will result in an error.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews