What is the difference between `var` and `const`?

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?

15 Likes

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

2 Likes

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.

7 Likes

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

3 Likes

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.

1 Like

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.

:crazy_face:

1 Like

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.