Learn JavaScript With Me-I

Rahul Kumar Das
3 min readAug 4, 2022

--

Understanding JavaScript(JS) Variables

With this, we will start our JavaScript(JS) Series of learning with me.

Today we will be going to look at JavaScript(JS) variables.

When we write programs, we deal with data and use them we need to store them somewhere.

Variables are those containers that store the data value. so when you need that data anywhere in your program, you don't need to address the whole data every time repeatedly but the name of the variable.

They're simply a name to the location.

Suppose we are creating a program where age is required. To work with age, we’ll create a variable and store the age in it.

16 is the data that we’ll be using in the program. But we cannot use it directly like 16 or “16”, this way program will not be maintained properly and the code will be unreadable. Also, we won’t be able to change the user’s name when needed.

You can initiate the variable at the time of declaration as well as later in the program.

So it is advised to create a variable and store our data in it.

This way if you want to change the data of the age variable, you just need to change the value of the age variable.

Changing the age value from ‘16' to ‘18’ changes the value in the memory as well.

In JavaScript, there are 3 keywords to declare a variable

VAR

Variable declared with var keyword are either local or global to the function.

If they are declared inside a function they are local to that function and if declare outside the function then they are global variables.

The var keyword is used in pre-ES6 versions of JS

More about pre-ES6 versions of JS

LET

Variable declared with let keywords are local to the block they're declared in. let is the preferred way to declare a variable when it can be reassigned. These variables unlike the one with the var keyword cannot be redeclared.

CONST

Variables declared with the const keyword are the ones that never change their value throughout the program. If you know in prior that some value to the variable won’t be changed throughout the program, declare it with the const keyword to reduce the mistakes.

UNDEFINED

Variables that have not been initialized.

Extra

The typeof keyword returns the data type of a value.

We use the console.log() method to log the value inside the round brackets() to the output screen.

Semicolons are not mandatory in JavaScript, but most people add them as it is a good practice.

Next part — (link)

--

--