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
?
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?
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.
Well, I advocate for no globals so I need to disagree with myself and agree with you both.
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:
console.log(x);
let x;
This errors because x
is not defined.
console.log(x);
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 google helps with that:
@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?
Prism.highlightAll();
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.