🔥What are the differences between const, let, and var? 10 Must-Know Questions for Junior Developers💡🚀
Tags: javascript, career, interview, tutorial
Published: 2023-09-27 15:17:00

In JavaScript, you can declare variables using `var`, `let`, and `const`. They differ in terms of scope, hoisting, and reassignment.

**Var**

Before ES6, `var` was used to declare variables. `var` variables are either globally scoped or function scoped. A `var` variable is globally scoped when declared outside a function and can be accessed anywhere in the code. When declared inside a function, it's function scoped and can only be accessed within that function.

```jsx
var greeter = "hey hi"; // globally scoped

function newFunction() {
    var hello = "hello"; // function scoped
}
console.log(hello); // error: hello is not defined

```

In terms of reassignment, `var` variables can be updated and re-declared within the same scope without any error.

```jsx
var greeter = "hey hi";
var greeter = "say Hello instead"; // No error

```

`var` variables are hoisted to the top of their scope and initialized with a value of `undefined`[Source 0](https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/).

**Let**

`let` is preferred for variable declaration since ES6. A significant difference between `let` and `var` is that `let` is block-scoped, meaning it can only be accessed within the block where it's declared.

```jsx
let greeting = "say Hi";
if (times > 3) {
    let hello = "say Hello instead";
    console.log(hello); // "say Hello instead"
}
console.log(hello) // error: hello is not defined

```

A `let` variable can be updated but not re-declared within its scope.

```jsx
let greeting = "say Hi";
greeting = "say Hello instead"; // No error

let greeting = "say Hi";
let greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared

```

`let` declarations, like `var`, are hoisted to the top. However, unlike `var`, `let` is not initialized, leading to a Reference Error if you try to use a `let` variable before declaration[Source 0](https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/).

**Const**

`const` is another way to declare variables. `const` declarations are block-scoped like `let`.

```jsx
const a = 10;
if (true) {
    const a = 9;
    console.log(a); // 9
}
console.log(a); // 10

```

The main difference between `let` and `const` is that `const` variables cannot be updated or re-declared.

```jsx
const greeting = "say Hi";
greeting = "say Hello instead"; // error: Assignment to constant variable.

const greeting = "say Hi";
const greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared

```

However, while a `const` object cannot be updated, the properties of this object can be updated.

```jsx
const greeting = {
    message: "say Hi",
    times: 4
}

greeting.message = "say Hello instead"; // No error

```

Like `let`, `const` declarations are hoisted to the top but are not initialized[Source 0](https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/)[Source 2](https://www.geeksforgeeks.org/difference-between-var-let-and-const-keywords-in-javascript/).

In summary:

- `var` declarations are globally scoped or function scoped, can be updated and re-declared within its scope, and are hoisted to the top and initialized with `undefined`.
- `let` declarations are block-scoped, can be updated but not re-declared within its scope, and are hoisted to the top but not initialized, and you can not use it in block-scope.
- `const` declarations are block-scoped, cannot be updated or re-declared, and are hoisted to the top but not initialized.

---

> I would love to share a part of my preparation tips for job interviews.
> 

As a React developer, I'm currently on the lookout for new opportunities. If you know of any roles where my experience could be a good fit, I would love to hear from you.

You can reach out to me anytime at my email [xMohammedAwad@gamil.com](mailto:xMohammedAwad@gamil.com), or connect with me on [LinkedIn](https://www.linkedin.com/in/xMohammedAwad/). Check out my projects on [GitHub](https://github.com/xMohammedAwad) to see more examples of my work.