DEV Community

Marlon Munoz
Marlon Munoz

Posted on

1

let, var or const, what's the difference?

let, var , const? when do we use this variables really?
Back in 2018, I got the opportunity to play around a little bit with JavaScript. During that year, I was thrilled to learn how this language performs. The first thing I learned was to declare a variable. My first variable was var name = 'el marlo' after I so this variable be used within my function and console.log(name) // => el marlo. As a beginner programmer, I got excited. Fast forward to 2024, officially starting my journey into Software Engineering, getting my hands into JavaScript after so many years and a lot of this have changed. The first thing I noticed, there were more options to declare a variable? What is let and const, I was only familiar with var.

var

var is the oldest keyword for declaring variable. Therefore, let's address the difference against the other two: let and const to help us decide which one should go into our code.

I learned that var was a keyword that if you are planning to use, to be very careful or don't use it at all due to a lacking in block scope or in plain english, the code that goes inside the curly braces {}. In addition, it can create bugs in your code because var variables can be re-declared and updated:

var favHobby = "Eskate";
var favHobby = "Sleeping";
var favHobby = "Joking";

console.log(favHobby); // => Joking
console.log(favHobby); // => Joking
console.log(favHobby); // => Joking

Enter fullscreen mode Exit fullscreen mode

let

letis the update version of var. This variable is blocked scoped, which means that unlike var, anything that we declare within {curly braces will only be available within this scope}:

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // Expected output: 2
}

console.log(x);
// Expected output: 1

Enter fullscreen mode Exit fullscreen mode

Example from: mdn web docs
In addition, let can be updated but NOT re-declared.

const

const is the more reliable variable to used for the following reasons: const declarations are block scoped: which means it is only accessible {within the block}. Another strong reason is, const cannot be updated or re-declared unless it is an object. If const was an object, then properties could be added, removed or updated.

const number = 42;

try {
  number = 99;
} catch (err) {
  console.log(err);
  // Expected output: TypeError: invalid assignment to const 'number'
  // (Note: the exact output may be browser-dependent)
}

console.log(number);
// Expected output: 42

Enter fullscreen mode Exit fullscreen mode

Example from: mdn web docs

đź‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

đź‘‹ 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