DEV Community

Start Javascript: learn about variables

Nathanaël CHERRIER on September 04, 2019

Variables are the absolute base of programming. We wouldn't be able to do much without variables. We can find it in all the advanced concept of p...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

const defines a constant. Constants are not variables. The clue is in the names:

  • 'constant' - a place to store a value that once set, remains CONSTANT
  • 'variable' - a place to store a value that remains VARIABLE, i.e. can later be used to store another value
Collapse
 
robbevwinckel profile image
robbevwinckel

In Javascript const is a bit misleading. It does not define a constant, but it's a one-time assignment. It's a constant reference to a value.

Because of this you can not change constant primitive values, but you can change properties of a constant object.

Collapse
 
mindsers profile image
Nathanaël CHERRIER

Most people don't understand that when it comes to learning, the most important is not be absolutely true but to make connection between notions.

Can you tell me why almost every article/tuto/course I found by googling "What's a variable" explains constant as "a type of variables"? By doing that we tell the readers that what he just learned with variables is still valid for constants but one thing changes: he can't change the value of a constant. He doesn't have to know more when he just started to learn to code.

Also, what I find interesting in the JS documentation is the sentence:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

It seems like, here, a constant is a variable.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Not at all - it is a constant holding (or having a reference to) one 'thing'. The fact that the thing can be changed internally doesn't mean it isn't still the same 'thing' (like if I change my shoes from blue to red, I'm still me). The 'thing' that the constant is 'storing' cannot be changed to a different 'thing'.

This is the fundamental difference between 'variables' and 'constants' and should be taught right from the start so as to avoid any confusion later. It isn't difficult, the names describe the behaviour. A variable is variable, a constant stays constant

Thread Thread
 
mindsers profile image
Nathanaël CHERRIER

The 'thing' that the constant is 'storing' cannot be changed to a different 'thing'.

This is the fundamental difference between 'variables' and 'constants' and should be taught right from the start so as to avoid any confusion later.

When the article say:

The value you'll give to a constant is the only value it will stock until its deletion.

It's not clear enough?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

The issue is with referring to a constant as a variable. This is simply wrong and generates unnecessary confusion. Your description of the behaviour of a constant however, is completely acceptable

Thread Thread
 
mindsers profile image
Nathanaël CHERRIER

Then, you can start a proposal to change that in the TC39 and Ecma 262 specifications because they are currently referring to a constant as a variable:

let and const declarations define variables that are scoped to the running execution context's LexicalEnvironment.

source: tc39.es/ecma262/#sec-let-and-const...

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

A javascript specification (that defines at a technical level the way a const is implemented in the language) and how best to teach programming concepts to beginners are 2 entirely different things :)

Collapse
 
volomike profile image
Mike Ross 🇺🇸

Here's something fun to show people. Watch how long the JSON string becomes between an array and an object in these two scenarios:

var a = [];
a[1111] = 1234;
a[2222] = 1234;
console.log(JSON.stringify(a));

as opposed to:

var a = {};
a[1111] = 1234;
a[2222] = 1234;
console.log(JSON.stringify(a));

Key point -- you'll often want to use objects instead of arrays.

Collapse
 
curtisfenner profile image
Curtis Fenner

I'm going to be nitpicky.

Variables in JavaScript do not have types. They have values, and those values have types. This is important to understand JavaScript, as it is to understand other dynamically typed languages; there is no restriction on which types of values any given variable can take on.

The JavaScript engine is capable of guessing (or "infer") the type of a variable based on its value

JavaScript does not do any inference, as it has no static type system. This is called reflection, and it is not a guess -- the language is mandated to keep the bookkeeping records which make it possible to determine, dynamically, what the type of any value is (again, not variable, but value).

Collapse
 
mindsers profile image
Nathanaël CHERRIER • Edited

Thanks for reading and sharing your thoughts. I really appreciate.

Talking about if it's the variable or its value that has a type is like asking if it's the egg or the chicken that appears first. Given that in JavaScript a variable always has a value and a value always has a type, I think we can say a variable has a type even if this type can change (when the value change though).

Collapse
 
kritikgarg profile image
kritikgarg • Edited

👌 This is a fantastic introduction to variables in Javascript, Nathanaël! 🙌You've done an excellent job of breaking down the concept in a way that's easy to understand, making it a valuable resource for those new to the language. I'm looking forward to reading more of your insightful posts in the future!

👀 Recently, I came across an 📰 informative article at Guide to Salesforce Flow Variables.💡 Found it to be a 💯 great resource for those interested in learning about flow variables in Salesforce! 🌟It provides a complete guide on flow variables, which is really helpful for beginners. Highly recommended!