DEV Community

Cover image for 🔥What are the differences between const, let, and var? 10 Must-Know Questions for Junior Developers💡🚀
Mohammed Awad
Mohammed Awad

Posted on

🔥What are the differences between const, let, and var? 10 Must-Know Questions for Junior Developers💡🚀

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.

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

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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

var variables are hoisted to the top of their scope and initialized with a value of undefinedSource 0.

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.

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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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 declarationSource 0.

Const

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

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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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

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

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

Enter fullscreen mode Exit fullscreen mode

Like let, const declarations are hoisted to the top but are not initializedSource 0Source 2.

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, or connect with me on LinkedIn. Check out my projects on GitHub to see more examples of my work.

Top comments (8)

Collapse
 
rsaz profile image
Richard Zampieri

Regarding your topic, I created a repo called Typescript Coding Guidelines, and in the first section I talk about the use of let, var and const. GitHub Repo

Variable Declaration

To declare a variable you can use the keywords var, let or const.

  • const: increase the predictability of your code, that intrinsically improve performance.

  • var: is globally scoped, doesn't respect code blocks, this may sometimes generate confusion. Avoid var when you can.

  • let: is scoped constrained. If you declare a variable inside of a scope that variable will exist only inside of the scope where it was created. You can't access the variable externally. This is usually the strategy used in most of the strongly typed languages.
    Prefer const over let when possible, and avoid using var.

Collapse
 
xmohammedawad profile image
Mohammed Awad

nice, short words but meaningful. but var is valid sometimes I don't know why everyone hates it. it's globally scoped so that one uses it, and sometimes you want to use var inside if or nested for loop and still need outside the nested loop so you can not use let there. also with var, you can re-declare. I know it's seems stupid 😂
but who knows maybe someone gets pinefite from that. right?

Collapse
 
clateman profile image
Clayton Malarkey

very good lesson

Collapse
 
xmohammedawad profile image
Mohammed Awad

thanks, glad to hear that

Collapse
 
clateman profile image
Clayton Malarkey

what you up to tonight

Thread Thread
 
xmohammedawad profile image
Mohammed Awad

what do you mean? nothing I guess

Collapse
 
kalkwst profile image
Kostas Kalafatis

Hi! #discuss posts should be questions designed to elicit community responses. Since this is more of a blog post than a question, please consider removing the #discuss tag. Thanks!

Collapse
 
xmohammedawad profile image
Mohammed Awad

ok, I will learn more about other tags