Let, Var and Const – What's the Differences?

Let, Var and Const – What's the Differences?



The var Keyword

The var keyword has been around since the inception of JavaScript and is known for its function-scoped nature. Unlike block-scoped variables, var variables are either globally scoped if declared outside any function or function-scoped if declared inside a function.

Example of var:


In the example above, the variable topic declared with var inside the if block is accessible outside the block but still within the same function. This is because var does not adhere to block scope; instead, it is scoped to the nearest function.

The Problem with var

The lack of block scope can lead to unexpected behavior, particularly in loops and conditional statements. Consider the following example:



One might expect this code to log 0, 1, 2, 3, and 4 to the console. However, it prints 5 five times. This is because var is function-scoped, and by the time the setTimeout callback executes, the loop has completed, leaving i with a value of 5.


The let Keyword

To address the shortcomings of var, ES6 introduced let, which is block-scoped. Variables declared with let are confined to the block in which they are declared, and they are not accessible outside of that block.

Example of let:


In this example, the variable course is declared inside a block using let. Attempting to access course outside the block results in a ReferenceError because let is block-scoped.

let in Loops

One of the most common uses of let is in loops, where it helps avoid the pitfalls associated with var:

This time, the code logs 0, 1, 2, 3, and 4 as expected. Each iteration of the loop creates a new block scope for i, ensuring that the correct value is logged by the setTimeout callback.

The const Keyword

The const keyword, like let, is block-scoped. However, it has an additional constraint: variables declared with const cannot be reassigned after their initial assignment. This makes const ideal for declaring constants or values that should not change.

Example of const:

In this example, the variable course is declared with const, and any attempt to reassign it results in a TypeError. However, it's important to note that const does not make the value itself immutable—only the binding is immutable.

Example with Objects:

Here, although person is declared with const, we can still modify the properties of the object because const only prevents reassignment of the variable itself, not its contents.

Conclusion

Understanding the differences between var, let, and const is crucial for writing clean and bug-free JavaScript code.

要查看或添加评论,请登录

Thi?u Quang Khoa的更多文章

  • JavaScript Interview Questions (Part 4)

    JavaScript Interview Questions (Part 4)

    61 What are the main rules of promise 62 What is callback in callback 63 What is promise chaining 64 What is…

  • JavaScript Interview Questions (Part 3)

    JavaScript Interview Questions (Part 3)

    41 What are the differences between cookie, local storage and session storage 42 What is the main difference between…

  • Top 400 JavaScript Interview Questions (Part 2)

    Top 400 JavaScript Interview Questions (Part 2)

    21 What is the Temporal Dead Zone 22 What is an IIFE (Immediately Invoked Function Expression) 23 How do you decode or…

  • Top 400 JavaScript Interview Questions (Part 1)

    Top 400 JavaScript Interview Questions (Part 1)

    No. Questions 1 What are the possible ways to create objects in JavaScript 2 What is a prototype chain 3 What is the…

  • Javascript: isFunctions (Part 2)

    Javascript: isFunctions (Part 2)

    5. isUndefined Hàm này ki?m tra xem giá tr? có ph?i là hay kh?ng.

    1 条评论
  • Javascript: isFunctions (Part 1)

    Javascript: isFunctions (Part 1)

    1. isNumber Hàm này ki?m tra xem giá tr? có thu?c ki?u và kh?ng ph?i là hay kh?ng.

    2 条评论
  • Javascript Interview Questions

    Javascript Interview Questions

    5 features of javascript What is DOM? Data types in javascript? Difference between let, var, const Difference between…

  • JavaScript Reversed Words

    JavaScript Reversed Words

    All Javascript Reserved Keywords The below list cannot be used as variable names, as they are reserved keywords. Most…

  • Using parseInt() and toString() for conversions

    Using parseInt() and toString() for conversions

    Trong JavaScript, vi?c chuy?n ??i gi?a các ??nh d?ng s? là m?t tác v? ph? bi?n, và có nhi?u ph??ng pháp ?? th?c hi?n…

  • Javascript: Splice vs Slice

    Javascript: Splice vs Slice

    Trong JavaScript, splice và slice là hai ph??ng th?c ???c s? d?ng ?? làm vi?c v?i m?ng, nh?ng chúng có m?c ?ích và cách…

社区洞察

其他会员也浏览了