WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Intermediate · question 21 of 100

Can you explain the differences between ’let’, ’const’, and ’var’ in TypeScript?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

In TypeScript (as well as JavaScript ES6+), ‘let‘, ‘const‘, and ‘var‘ are used to declare variables. They have different scoping rules and behavior, which I will explain in detail.

1. ‘var‘:

When using ‘var‘, the variable is function-scoped or globally scoped, depending on where it is declared. When a variable is declared using ‘var‘, it is "hoisted" to the top of its scope. Hoisting means that the variable declaration is moved to the top of the scope, but the assignment of the variable’s value remains in its original position.

Here’s an example illustrating hoisting:

function exampleFunction() {
    console.log(variable); // Output: undefined
    var variable = 5;
    console.log(variable); // Output: 5
}

In this example, the variable declaration ‘var variable‘ is hoisted to the top of the function, but the assignment ‘variable = 5‘ remains in its position. So, the code is executed as if it were written like this:

function exampleFunction() {
    var variable;
    console.log(variable); // Output: undefined
    variable = 5;
    console.log(variable); // Output: 5
}

2. ‘let‘:

The ‘let‘ keyword is used for declaring variables that are block-scoped, meaning their scope is limited to the nearest enclosing block, such as a loop or conditional statement. Unlike ‘var‘, variables declared with ‘let‘ are not hoisted, they also cannot be re-declared within the same scope.

Below is an example comparing variables declared with ‘let‘ and ‘var‘:

function exampleFunction() {
    var localVar1 = 1;
    let localVar2 = 2;

    if (true) {
        var localVar1 = 3; // localVar1 is re-assigned
        let localVar2 = 4; // localVar2 is declared within the new block scope

        console.log(localVar1); // Output: 3
        console.log(localVar2); // Output: 4
    }

    console.log(localVar1); // Output: 3
    console.log(localVar2); // Output: 2
}

3. ‘const‘: The ‘const‘ keyword, similar to ‘let‘, is block-scoped. The difference, however, is that variables declared with ‘const‘ cannot be re-assigned after they have been initialized, making them constant values.

Here’s an example using the ‘const‘ keyword:

function exampleFunction() {
    const localVar = 1;

    if (true) {
        const localVar = 2; // localVar is declared within the new block scope

        console.log(localVar); // Output: 2
    }

    console.log(localVar); // Output: 1
}

In summary:

- ‘var‘: function-scoped or globally scoped, can be re-assigned, and hoisted.

- ‘let‘: block-scoped, can be re-assigned, but not hoisted.

- ‘const‘: block-scoped, cannot be re-assigned, and not hoisted.

For best practices, it is recommended to use ‘const‘ when declaring variables that don’t need to be re-assigned and ‘let‘ for other scenarios. Avoid using ‘var‘ since it has less predictable behavior due to hoisting and lack of block-scoping.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics