Understanding Variables in JavaScript

Nathan PreciousNathan Precious
3 min read

Understanding Variables

Declaring and Re-declaring Variables

Understanding Let, Const and Var

Local and Global Variables

Hi there besties,

It's been a while on this space but I'm back and better than ever. I try to simplify concepts in Frontend Development to the best of my ability so you can understand and get to write codes too.

Let’s get into it on Variables, shall we?

Understanding Variables

Variables in JavaScript are containers for storing data values.
Now, before you roll your eyes and scroll past thinking it's the same content you read online and barely understand, give me a chance to break this down for you.
Let's go,
When you hear Variables in JavaScript, think of it like a cupboard with different spices and herbs in containers and labels on them.

Whenever you open your pantry and you need a spice like thyme, all you do is look for the tag on the bottle containing the spice and take it out. You could even pour out the contents of the bottle straight into your dish with your eyes closed because you know it reads thyme and that's what you put in it earlier.

Now let's apply this to JavaScript,
The variables are the containers that hold the content.
The unique identifier is the name of the spice on the container and
The Data Type is the content the container holds.

Declaring a Variable

Declaring a Variable is another way to say Creating a variable.
You can leave a variable undefined and assign a value to it later in the course of your code.

let last name; last name = 'Johnson';

You can still assign a value to a variable directly:

let last name = 'Johnson';

Also, you can assign many variables in one statement:

let firstName = 'Jane', last name = 'Johnson' , age = 23;

Difference between Var, Let and Const

A variable declared with var is accessible within the function it was declared in and also globally if it was declared globally.
Don't lose me here, I'll explain Local and Global variables in this article. Follow through
Now this is not the same with the Let and Const, unlike the var, any variable assigned to Let or Const is accessible only within the block it was declared in.
Look at this

var userName = 'Jane'; let userName = 'Joy';

Between Let and Const

Const is used when you do not want the value you assigned to a variable to change.
E.g a formula, a constant value

const pi = 3.142;

You cannot assign another value to pi throughout your entire code.
Let on the other hand can be used when you know the value assigned to the variable can be changed.

let userName; userName = 'Danielle';

Along your line of code, you can assign a new value to the "userName" variable.

Local and Global Variables

Global variables can be defined anywhere in your JavaScript code. It's the normal way of declaring a variable in code space.

let age = 21;

I hope this explains Variables in JavaScript to you.

In my next article, I'll talk about Data Types and Arithmetic Expressions and we can build up from there.

Ciao.

0
Subscribe to my newsletter

Read articles from Nathan Precious directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nathan Precious
Nathan Precious

I am a Frontend Intern. The tech world is really fascinating, I want upcoming devs to see how easy it can be. ...sharing my tech experience to inspire upcoming devs. #anybodycancode