Why shouldn't I use var in JavaScript?

Continuing the discussion from Chat with your friends!:

Why not to use var variables?


IIRC var is an outdated standard, let and const are the new ones.


What is the difference between var and let?

1 Like

var is outdated, var creates a variable in global scope and is sort of hacky and just generally a no no while let (and const) creates a variable in local scope.


Shouldn’t I be able to mark yours as solution?

1 Like

Not when it was in #general, now that it’s in #code-help:html-css-js you can :slight_smile:


They’re the same thing lol

Did you look at the solution? They are not.

Yes but var is not outdated, it still has its uses.

It is outdated because any uses you find would be hacky solutions.
If you find a situation that you think you have to use var chances are you were doing something very wrong to get to that point.


I would have to disagree with that, I agree with what @CSharpIsGud said

Well, I advocate for no globals so I need to disagree with myself and agree with you both.

1 Like

Also just to add the final nail to the coffin, people don’t hate var just because it defines things out of its local scope. It’s because of this:


let x;

This errors because x is not defined.


var x;

This prints undefined


Most of cases it is advisable to predominantly employ the let and const keywords in your codebase, as they offer enhanced safety through stricter scoping rules. However, in scenarios where you are implementing minor interactivity within a web page, it is acceptable to utilize var without any qualms.

By embracing let and const, you fortify your codebase with more robust scoping mechanisms, preventing inadvertent variable hoisting and enabling better control over variable lifespan. The let keyword establishes block-level scope, ensuring that variables are confined within the specific block in which they are defined. This is particularly useful in preventing accidental variable shadowing and enhancing code readability. On the other hand, const guarantees immutability, serving as a powerful tool when you need to declare constants that remain unchanged throughout your program’s execution.

Despite the advantages presented by let and const, it’s essential to acknowledge that there is no inherent flaw in employing var, especially when dealing with smaller-scale interactivity within a web page. In such limited contexts, the looser scoping rules associated with var do not pose significant concerns and may simplify the development process. Doing simple is also beautiful.

If somebody wont know what all these fancy terms means and want to understand better how javascript actually work to be 10X javascript dev :disguised_face: google helps with that:

  • variable hoisting
  • variable lifespan
  • variable shadowing
  • block-level scope
1 Like

@whileTRUEpass @MattDESTROYER I think that globals are still something that works.

I’ve seen guides on PrismJS and it says that if you want to regularly highlight it, use this:

window.Prism = window.Prism || {}; // Globals, right?

There’s really no other solution for that, I mean, this would probably work too?

var Prism = Prism || {};

But this wouldn’t work! It wouldn’t create it on the window object, would it?

let Prism = Prism || {}; // Error!

So I think there are still a few uses for var and globals.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.