hello, please whats the big difference between variables and const
const PI = Math.PI
That is the algebraic constant, PI expressed as a constant in our code.
const MOL = 42
A theoretical numeric constant derived from philosophy. (Tongue in cheek.)
const myPermanentDataStructure = []
Given a const
declaration, this array may be populated, depopulated, manipulated and mutated, but it cannot be destroyed. It’s a permanent data structure in our program.
There is no practical reason to use const
when let
will suffice. Sure it will work, but it will also betray the gray area of one’s understanding.
Forget var
. Although its relaxed scope definition can be handy we really should learn code patterns that will give let
the same or similar flexibility. var
is old school, and on its way out.
const mySiteURL = "https://example.com/"
Makes sense, and even as a global constant. Constants should make sense, or why use them?
From the albeit limited research I’ve done to date, const
and let
have the exact same scope when declared within a code block, and are treated no differently by garbage collection. The Codecademy lesson on variables in the first introductory JavaScript course instructs us to use const
unless the variable will need to be changed. This seems to contradict your advice to use let
unless there is a reason to use const
.
From the course, Getting Started with JavaScript:
let
is the preferred way to declare a variable when it can be reassigned, and const
is the preferred way to declare a variable with a constant value.
https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-introduction/lessons/variables/exercises/review-variables
In the confines of a small block, I see no reason to fret over the use of either const
or let
. The variables are inconsequential in the grand scheme of things. I wouldn’t put too much stress on whether I contradict the lesson, or not. Follow your own reasoning. Don’t try to carve too much in stone.
Hello,
Variables declared with the var
keyword can not have Block Scope .
Variables declared inside a block {} can be accessed from outside the block.
Variables declared with the let
keyword can have Block Scope.
Variables declared inside a block {} can not be accessed from outside the block.
You can fine more about here
Please update this answer, its missing key feature:
let and const are block scoped, while var
is function scoped. Understanding these scope rules can still justify using var
(won’t happen often, but there can still be use cases).
when a constant variable can be on object or array, then the content of the array or object can be changed/updated. What we can’t do is re-assign a different value to the variable. This is a bit difficult to deduce from your answer.
the diff between “var” and “const” is the new implementation of declaring variables in ES6.
var is outdated one way of declaring a variable:
var myOutdatedVariable = value;
however, with the new way of variable declaration (ES6), you have two options;
the “let”, which is used to declare a variable which the value can change within the application
let myNewDynamicVariable = value;
while with the “const”, the variable is the value of the variable is permanent within that application.
const myVariableValueThatNverChange = value;
also to note:
“let” and “const” are block scoped, while var
is function scoped. Understanding these scope rules can still justify using var
(won’t happen often, but there can still be use cases).
As soon as you get used to var, const, and let in JavaScript go try and learn Swift. In Swift var
is like JavaScript’s var
and let
and Swift’s let
is like JavaScript’s const
.
In BASIC we could not outright declare a variable, like, a = 0
. We had to bring it into existence using LET
. Once in existence, the variable could be assigned any value at any time, regardless of type. It is all so removed by now.
Every language will have its nuances and syntax. It doesn’t make it easier to learn either when we try to compare languages or learn one we will never use. Not everyone is into Apple.
I’m gonna have to say there is one practical reason to prefer const
to let
, or rather to default to const
over let
- if you get in the habit of doing so, it cuts down on potential errors coming from having unexpectedly reassigned a variable elsewhere. In general, prefer immutability where you can, it tends to help cut down on semantic errors.
This was advice I got from a full time web dev - obviously there’s personal preference involved, but you can always change things to let
as needed, but nobody ever goes back and changes their unmutated let
s to const
s (unless you have a linter checking this) , so if you’re not sure and don’t wanna spend time thinking about it, just start everything off as a const
and change it later if you have to.
almost like a unique identifier in the sense that it is mostly literally constantly going to equal x? If you know something will only be this thing then we can use constant. If there is another grab into the formula somewhere else maybe we use let. And Var seems vague like using the var to represent any variable when we can be more specific and use the variable as a functioning variable. i view var as a noun or a dogear when const or let are a verb or describe what to do with the value with argument.
. . .
Is that some kind of techno speak?
Thank you. I will work on my vocabulary. And double check my post before posting it. (more like tech-house speak tho )
More like 'Heck-NO!'
I’m just making light of the noticed comment tho I am embarrassed of my comment. I will tho try harder on leaving comments. Thank you for your feedback it is much appreciated. I enjoy your comments and replies on Codecademy. You are very knowledgeable and I look to you for inspiration.
Hej,
For a beginner (as my self), it is quite well explained under this video in youTube:
var, const, let differences explained.