The ultimate guide to variables in JavaScript
Tags: javascript, tutorial, programming, beginners
Published: 2023-01-30 14:14:08


![potatoes] (https://images.unsplash.com/photo-1573196444577-af471298e034?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80, "potatoes")

It was a day of potato harvest. Working on the field all day isn't easy and you are ready to go home to your family. You sit down, sigh deeply, and enjoy a bit of fresh air.

Suddenly the king's herald appears. 

_"Hear ye, hear ye! His Majesty the King hereby declares that a new tax on all peasants, merchants and traders within the kingdom shall be imposed. The revenue from this tax shall be used to fund the king's big appetite. All merchants and traders are required to pay this tax in full, and failure to do so shall result in severe penalties."_

Quickly you collect all potatoes and make a run for it. No way you are giving the potatoes you stole, fair and square, to some king. You end up in the woods and hide behind a big bush thinking:
 
"_It would be nice to store all of this potatoes somewhere safe._"

And just like that you hear a voice:

Hey you, those are some nice looking potatoes. Would you like me to take care of them? 

## Var 

_"Who is that? Show yourself before I throw one of my potatoes on you."_

My name is **var**. I have been storing various things for peasants and kings for 20 years. But now it seems that the whole world has forgotten about me. 

_"Hmm... I'm kind of tired from carrying these potatoes. It would be nice to rest. Ok, var store them."_ 

```js
var potatoes = 20;
```
Thank you for this opportunity. Now if you want to retrieve them,  just say the magic word **console.log()**

_"Is there a way to protect them even more? It took me a long time to steal them."_

How about this?

```js
{
  var potatoes = 20;
}

console.log(potatoes);
```

### Output

```
20
```
_"So, no? You ain't block scoped? How am I going to protect them then?"_

I may not be block scoped but I am function scoped. 

```js
function protectPotatoes() {
  var potatoes = 20;
}

console.log(potatoes);
```

Now, no one can access them. Even our great queen and king could not get them. 

### Output 

```
ReferenceError: potatoes is not defined
```

In fact, I can even give them decoy potatoes and shadow your original potatoes.

```js
var potatoes = 10;

function protectPotatoes() {
  var potatoes = 20;
}
console.log(potatoes);
```
### Output

```
10
```

_"Ha! Does this happen because potatoes are globally scoped when they are defined outside the function? And now when they are inside the function, that is they are function scoped, they are only available inside that function block?"_

Well, for a peasant, you sure do know a lot of JavaScript. For that, I'm going to show you what else I can do with your potatoes. 

```js
console.log(potatoes);
var potatoes = 20;
```

### Output

```
undefined
```
You see, even though we haven't met yet, I believed in you. I knew you were going to bring me some potatoes. The way I see it is this: 

```js
var potatoes;
console.log(potatoes);
var potatoes = 20;
```
_"Aha. So you are hoisted at the top? I can just declare that there are going to be some potatoes in the future. I don't need to assign anything yet? But there's just one thing I have been wondering. What if someone does this: "_

```js
var potatoes = 20;
var i = 1;

if (i > 0) {
  var potatoes = 0;
}

console.log(potatoes);
```
Well, yes, now your potatoes aren't safe anymore. How are you only eating potatoes if you know so much JavaScript?

### Output

```
0
```

_"I think this isn't a very good trait of yours. You are quirky and untrustworthy."_

What did you just say to me? Say goodbye to your precious potatoes.

```js
var potatoes = 20;
var potatoes = 0;
console.log(potatoes);
```
### Output

```
0
```
_"Noooooooo, my potatoes. No wonder no one likes you. You can even redeclare."_

Tell you what, if you can guess what's going to happen now, I'm going to bring your potatoes back.

```js
console.log(potatoes);

function getPotatoes() {
  var potatoes = 20;
  console.log(potatoes);
}

getPotatoes();
console.log(potatoes);
```

_"Well, I guess that I am having french fries for dinner! The correct answer is 20."_

Ha. WRONG!!! What you get is nothing. You see, before the function is called, the first `console.log(potatoes)` is executed. But since there are no potatoes yet, we get an error. Also, what are french fries?

### Output

```
ReferenceError: potatoes is not defined
```
_"Like I would tell you... thief!!"_

## Let :chestnut:

As you walk through the woods, defeated, hopeless and potatoless, you come across a tree of chestnuts.
After fighting a squirrel, you are faced with the same problem.  A second voice appears:

Hail fellow, well met. Nice chestnuts you got there. What are you going to do with them?

_"Who are you? var, is that you again? Get away from me!"_

Oh easy there, my friend. I am not var. My name is **let**. I am var's cousin. I can store those chestnuts for you:

```js
let chestnuts = 50;
```

_"var did the same thing before she stole my potatoes. Now I am forced to eat chestnuts for dinner. I don't know if you noticed but I am not a squirrel."_

Really? And what about those two very long front teeth? It's unfortunate that you ran into var. But I can assure you that I am different.

_"Oh, really? And why should I trust you?"_

Because I can protect your chestnuts: 

```js
{
  let chestnuts = 50;
}

console.log(chestnuts);
```

##Output

```
ReferenceError: chestnuts is not defined
```

_"Now this is what I want. So you are block scoped, unlike that malicious var. What else can you do? Can you hide them inside a function?"_

Well, yes that runs in our family. 

```js
function protectChestnuts() {
  let chestnuts = 50;
}

console.log(chestnuts);
```

### Output

```
ReferenceError: chestnuts is not defined
```

_"And what about decoy chestnuts? Can you do something like this?"_

```js
let chestnuts = 10;

function protectChestnuts() {
  let chestnuts = 50;
}
console.log(chestnuts);
```

Yes, I've also inherited this from var. 

### Output

```
10
```
_"And what about this?"_

```js
console.log(chestnuts);
let chestnuts = 20;
```

Well, no I can't do that. I have to be sure there are chestnuts before I can show them to you. I am not as gullible as var.

### Output

```
ReferenceError: Cannot access 'chestnuts' before initialization
```
_"Aha. I guess you also don't have a problem with this:"_

```js
let chestnuts = 50;
let i = 1;

if (i > 0) {
  let chestnuts = 0;
}

console.log(chestnuts);
```

Don't worry, even if someone has number 10, they won't be able to steal your chestnuts, I am block scoped, as you said. 

### Output

```
50
```

_"There's got to be something wrong with you."_ 

Well, yes there is, but I don't like talking about it. Since your teeth remind me of Mary, I'll tell you anyway.

_"Who's Marry? Your mother?"_

No, she was my rabbit. Anyway, I can't do this:

```js
let chestnuts = 50;
let chestnuts = 0;
console.log(chestnuts);
```

### Output

```
SyntaxError: Identifier 'chestnuts' has already been declared
```
_"Aha, so you can't steal them. Hey, look, another chestnut. Please store this one as well."_

```js
let chestnuts = 50;
chestnuts += 1;
console.log(chestnuts);
```
### Output

```
51
```

Well, now that I look at them, I want them for myself. While I can't redeclare, I can reassign.

```js
let chestnuts = 51;
chestnuts = 0;
console.log(chestnuts);
```

### Output

```
0
```

_"Noooooo, you give them back. I thought we were friends. Don't I remind you of your rabbit?"_

We ate that rabbit. Bye now.

## Const :mushroom:

So here you are again, all alone in the woods, your stomach growling, potatoless, chestnutless. You walk down to the creek and find a bunch of mushrooms. Quickly, you take as much as you can and think:  

_"Although I like stealing, I don't like it when someone does it to me. Is there anyone I can trust?"_

You hear another voice, which makes you wonder where all of these voices are coming from?

Yo, what's good y'all! It's your boy **const**, the one and only true variable straight outta JS. I'm here to spit fire and drop some knowledge.

_"Can you store this for me and not steal it?"_

Ha, I guess you've met var and let. Not very nice, are they? But don't worry. 

```js
const mushrooms = 15;
```
_"And what if someone tries to do this?"_

```js
{
  const mushrooms = 15;
}

console.log(mushrooms);
```
I got you, rabbit-man. Don't worry. Your mushrooms are safe.

### Output

```
ReferenceError: mushrooms is not defined
```

_"I have been cheated more than once. What about this?"_

```js
function protectMushrooms() {
  const mushrooms = 15;
}

console.log(mushrooms);
```
No way, I ain't like that. I am guarding your 'shrooms. We good.

### Output
```
ReferenceError: mushrooms is not defined
```
_"So you are also block scoped. Can you do this: "_

```js
const mushrooms = 2;

function protectMushrooms() {
  const mushrooms = 15;
}
console.log(mushrooms);
```
For sure. If you want them 'shrooms, tough luck.

### Output

```
2
```
_"And you can't steal them? What about reassigning and redeclaring?"_

```js
const mushrooms = 15;
const mushrooms = 0;
console.log(mushrooms);

const mushrooms = 15;
mushrooms = 0;
console.log(mushrooms);
```
Can't do that, ain't no way.

### Output

```
SyntaxError: Identifier 'mushrooms' has already been declared
TypeError: Assignment to constant variable.
```
_"Finally, I have found a friend. Oh look, another mushroom. Please store this one as well."_

```js
const mushrooms = 15;
mushrooms +=1
console.log(mushrooms);
```
I ain't with it, even for you, dawg, I'm out. I only keep values that don't change.

```
TypeError: Assignment to constant variable.
```

_"What? I am deeply offended. Now I am starting to miss var and let."_

You didn't just say that. Oh, I am hurt. I bet let and var didn't show you that we can do this:

```js
const mushrooms = {
  amount: 15,
};
mushrooms.amount = 0
console.log(mushrooms);
```
### Output

```
{ amount: 0 }
```

_"Nooooooooo. Not again. I thought you can't reassign values!"_

Well, I can't except when it's objects. I kind of dig their vibe. 

## Wrap-Up

After a whole day's work, you go home potatoless, chestnutless, mushroomless. 

Out of all the voices you've met, **var** is the most relaxed one, it's function scoped. Storing values inside var is not very safe since those values can be reassigned and redeclared. 
**Let** is a little bit more rigid since it doesn't allow redeclaring but it does allow reassignment. It's block scoped just like **const** which is the strictest of them all. It doesn't allow redeclaring or reassigning, but it does have a soft spot for objects.
 
Very useful knowledge for a medieval peasant, don't you think? :rabbit: