DEV Community

Cover image for let, const , var difference in Javascript?
sagar
sagar

Posted on

let, const , var difference in Javascript?

In JavaScript, let, const, and var are used to declare variables, but they are different in three ways:

1. Scope
2. Reassignment
3. Hoisting

1.Scope:

var is a functional scope means we access var variable anywhere within the function if we try access it outside function it will show error undefined
Example:-


function demo(){
  if(true){
    var n = 3;
  }
  console.log(n)
}
console.log(n) //ReferenceError: n is not defined
demo();
Enter fullscreen mode Exit fullscreen mode

let & const are block means we can access them within the scope only otherwise it will show undefined error
Example:-

function demo(){
  if(true){
    let n = 3;
    const m = 5;
     console.log(n) // 3
     console.log(m) // 5
  }
  console.log(n) //ReferenceError: n is not defined
  console.log(m) //ReferenceError: n is not defined
}
console.log(n) //ReferenceError: n is not defined
console.log(m) //ReferenceError: n is not defined
demo();
Enter fullscreen mode Exit fullscreen mode

2.Reassignment

  • var: Can be reassigned and redeclared within its scope.
  • let: Can be reassigned but not redeclared within its scope.
  • const: Cannot be reassigned or redeclared. The variable itself is immutable, though objects and arrays assigned to const can still be modified .

3.Hoisting

  • var is hoisted, meaning it's accessible before its declaration, but its value will be undefined until the code reaches the line where the variable is initialized.
  • let is also hoisted, but unlike var, it cannot be accessed before its declaration due to the "temporal dead zone."
  • Variables declared with const are also hoisted but must be initialized at the time of declaration and cannot be reassigned. However, if the variable is an object or array, its contents can be modified (e.g., adding or removing items from an array).
// var example
console.log(a); // undefined (due to hoisting)
var a = 10;
console.log(a); // 10

// let example
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;
console.log(b); // 20

// const example
const c = 30;
c = 40; // TypeError: Assignment to constant variable
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay