A lot of shiny new features came with ES2015 (ES6) and since it's 2017, it's assumed that a lot of JavaScript developers have become familiar with ...
For further actions, you may consider blocking this person and/or reporting abuse
this is a good article..however you should update your last example
const flow = {
message : "say Hi",
times : 4
}
to
const greeting = {
message : "say Hi",
times : 4
}
don't know how anybody who's reading this article missed that!
I noticed something did not add up but I didn't pay attention to the details since I got what I was looking for. Good catch, tho.
left me in blanks... I am glad you had pointed it out! Thanks'
and btw, the article is indeed one of the best!
thank you
It's updated now. Thanks for notifying
Yeah
For a noob like me, this explained "let" declarations for me.
I'm glad it did.
Good post. I wasn't sure about these. Just knew that 'var' should be avoided.
I think you have typo here. Remove the second 'let' from the first code block. :)
So while this will work,
let greeting = "say Hi";
let greeting = "say Hello instead";
this will return an error.
let greeting = "say Hi";
let greeting = "say Hello instead";//error: Identifier 'greeting' has already been declared
Thanks a lot for pointing that out. I'll correct it right away.
Thank for a great article
Error still exists in the article..
Why should var be avoided?
'Var' allows for re-declarations in code which does not throw errors and can create unintended side-effects in your code.
'Let' allows for variable reassignment but not for duplicate declarations (block-scoped), much like strongly typed languages like C# and Java.
'Const' allows for declaration once and for assignment once, and can never be re-declared or reassigned (block-scoped). For instance, I use 'const' for inline function declarations, so that I don't accidentally redefine this function's behavior at some later point in time by mistake. Even though I am unlikely to do so, it is just safer for myself and others to work on.
To paraphrase Sarah more generically:
"While it is not a problem if you knowingly want a var to be a certain value at a certain point in code, it becomes a problem when you do not realize that this same var has already been declared/defined before or, even worse, has had other functions operate on this var without your knowledge."
So, in other words, it's a much better idea to use 'let' and 'const' in order have safer control over your variables and constants.
This was incredibly helpful, and a great TLDR!
"Variable shadowing"
const alone does not guarantee protection for your data or let me say it does not protect your data from mutation,
Example:
Though the above code makes use of const i can still update the values via the code below
But this could easily be avoided by making use of
Object.freeze()
to freeze our const variables.Just add this line of code and you will discover that the values cannot be updated.
For more clarification check: freecodecamp.org/learn/javascript-...
Hi, great article but I think you have a mistake about the hoisting,you wrote:
"They are all hoisted to the top of their scope but while var varvariables are initialized with undefined, let and const variables are not initialized."
If you will look at the JavaScript Hoisting section in w3schools.com website,
you will notice that:"Variables and constants declared with let or const are not hoisted!"
Please check in:
w3schools.com/js/js_hoisting.asp
According to MDN and ECMA 2015 her words are right
check this developer.mozilla.org/en-US/docs/W...
That's exactly I m thinking the new let and const in javascript are not hoisted..she wrote wrong things..u should correct this article..do not mislead people with this ??
Years later, this is still the most concise explanation and welcomed even by those of us who are not lightweights - but don't stray into the JS badlands much. You have great teacher chromosomes, Sarah. Ignore the nit-pickers - this is still the best explanation primarily because of its brevity.
Thanks for the kind words, I really appreciate it.
The article is well written, but the problem is with the visualization of the concepts, if only these were depicted in pictorial or tabular format, can have immense effect on readers. Thanks for the article thought.
That's a great article. Inspired me to write:
Var is function-scoped and not block-scoped which means that constructs like if statements, while loops etc do not create new scopes while using var. It also means that, as var is function-scoped, if we do not create a function, we do not create new scope while using var.
Read More here:
Var vs Let in JavaScript
Hi, you have said that variable declared as var will have global scope if declared outside a function. But the same can even be said for variables declared as let and const. They indeed are locally scoped but when done outside a function, they will have global scope like var.
Please correct me if am wrong?
Thank you for your post, I am a newbie to Nodejs and quite confusing on when to use
let
,const
orvar
. As I saw some require a module withvar
while some withconst
, could you advise which is better when requiring a module? After reading your post, I thinkconst
is better to require another module because it will never change.One more question could you please do me a favor to explain the memory usage between these three, is there any different other than
scope
, thank youI've finally understood.
(Again) 'Cause I had forgotten hehe 😹😹
Thanks ♥
very helpfully, many thanks
Thanks for the explanations on the differences between var, let and const. When I first heard of let and const I was so confused. I wanted to know though is var still good to use and how should it be used when compared with let and const?
var erkan = 3;
if (erkan == 3) {
var erkan = 2;
console.log(erkan); //2
}
console.log(erkan); //2
//////////////
let erkan = 3;
if (erkan == 3) {
let erkan = 2;
console.log(erkan); //2
}
console.log(erkan); //3
//////////////
const erkan = 3;
if (erkan == 3) {
const erkan = 2;
console.log(erkan); //2
}
console.log(erkan); //3
Thanks, Good article on concept of using var, let and const.
Good article, if you are interested you could read my article on the same topic. Maybe an exchange of ideas :)
medium.com/notonlycss/difference-b...
Demystified the topic with your narration, Thank you.
Thank you so much, really cleared things up for me!
The best explanation that I see on Internet. Thanks!
Thank you for your kind words.
Thanks Sarah! Great, simple explanation.
let me clear about const.
const used when we don't want to reinitialize the variable values.
like
const a=[2,3,4];
a[1]++; //doesn't give error;
a=[3,4]; //give error;
that's about const.
The cleanest explanation I've seen so far of let vs. const vs. var.
Great article! Super thorough, but clear enough for a beginner like me to understand. Thank you!
Thanks Megan, I'm glad you like it.
I love the simplicity and consistency of the examples. They made the difference very clear. Thanks a lot.
In a global scope var will be associated with global object
where as let and const are not
ex: window.var_variable
not supported by let and const
Thanks for this article, it really helps me understand these basic concepts well.
Thanks a lot!
Excelent article, thank yoU!
absolutely nice article ,thumbs up
This was great; I learn a lot and tightened up my knowledge in 5 minutes 🙌🏾
Thank you so much. It was very easy to understand.
Awesome article. Very well structured and easy to understand.
Thank you.
nice :)
Some typos in there though. You have used an object flow accidentally. Two varying illegal examples of const look identical to me. Do check. Or did I miss the point. ☺. Cheers
Thanks
A good read Sarah! Thanks for letting me understand the big controversy between these 3 currently we have in our community 😁
Thanks for reading
A very good explanation indeed. I was impressed as you did not miss updating of const object properties since most of us might think that anything declared as const cannot be updated :).
Nice to know about var, let and const. Great article!
Great explanation, very easy to understand
Good Post.Thanks.
This piece makes sense a 101%. Thank you.
Just started Javascript and learned a lot from this article. Thanks a lot, great explanation!
Thanks a whole lot Sarah.
Thoroughly enjoyed the article.
It was easy to understand.
Kind regards.
Nice piece... ES6 all the way!
Thanks Chuks :)
If i declare an array as const variable , Can i update the array?
While you cannot reassign the array, JavaScript allows you to update it. Personally, I think that defeats the idea of using const. If you want to declare an array that will be updated, it might be more ideal to use let.
Thank you Sarah! It helped :)
Very well written.. Thanks.. It made this clear
Well explained!
Thank you for the article! I didn't know about variable hoisting or why using let and const was preferred over var. Excellent to know!
I'm glad I found you. No flowery language. Spot on. Thank you Sarah!!!
Excellent article! Thanks!
Great article , thank you
Super clear explanation -Thanks
Nice post! It's help me alot to understand the differences!
Very nice explanation and detailed, Keep it up!
Hi, Sarah! you wrote an amazing article, can I translate it to Portuguese to help others here in Brazil who doesn't know English?
Thanks ❤
Hi Raissa, that will be great! Please go ahead and do so. You can also send a link when you are done. Thank you.
Great Article :) . Thanks so much .
Thanks for this very simple explanation
Thanks, but how to use global let variable inside function
You cannot declare a global variable in a function with let.
This is great, THANKS!
Very well done! Neat article. Thanks!
Nice article.
Well written article, short and sweet summary of these three keywords.
Can you please explain me the 3rd point in hoisting of
const
what is a best way to make a declaration between var, let and const
perfect explanation for var, let and const!!
Well explained. thanks!
Very useful~
This post is still helping people. Thanks!
Thanks for this.
Thanks a lot Sarah, Very helpful.
Great article for "var - let - const" explanation. I am just learning Javascript and this guide clears out why I was seeing on other people code, for using the "let" and "const" instead of "var".
Good one, thanks so much!
Var, Let and Const in Javascript
youtu.be/R45FhbZl5SE
i now understand the use of let and const. thank you :)
Thank you for the great article, cleared out a lot for me.
I noticed that "let" are being initialized with undefined though, just like "var".