DEV Community

Cover image for What is the diffence between var, let and const?

What is the diffence between var, let and const?

Francisco Inoque on October 06, 2022

In JavaScript, there are different ways to declare a variable. These include using the keywords var, let, and const. Each of these has slightly dif...
Collapse
 
getify profile image
Kyle Simpson

I expected to eye-roll at yet another in a million "what is var/let/const" type blog posts. But this one was well done, and more balanced than most.

Collapse
 
frantchessico profile image
Francisco Inoque

Kyle, thank you so much for the feedback. And please don't forget to share.

Collapse
 
clydegrey profile image
Clyde

Variables declared with var are global variables, meaning they can be accessed from anywhere in your code.

This is incorrect. Simply declaring a function with var doesn't make it global at all. If you are in a function, a method or a module then you won't have access to that variable globally.

Collapse
 
frantchessico profile image
Francisco Inoque

Do you know what a global scope variable or function is?

It is any variable or function that can be accessed from any part of your code. But in this article, I'd like you to pay attention to the scope that each of the forms offers.

Collapse
 
clydegrey profile image
Clyde • Edited

Yes and you are incorrect. Simply using var doesn't make your variable globally scoped. Feel free to share code that shows a var being declared in a function being accessible in the global scope.

Thread Thread
 
bqardi profile image
Sune Seifert

I completely agree.
Doing this:

function namePersonal() {
  var fullName = "Francisco Inoque"
}
console.log(fullName)
Enter fullscreen mode Exit fullscreen mode

displays an undefined error an thus makes this statement incorrect:

Variables declared with var are global variables, meaning they can be accessed from anywhere in your code.

Collapse
 
thedenisnikulin profile image
Denis

Variables declared with const keyword are not immutable. You only cannot reassign the const variable, but you can modify fields of the object which this variable contains