Variables in JavaScript

Variables in JavaScript

Part 1

Table of contents

A variable is a container for a value. A variable is a “named storage” for data.

There are two types of variables in JavaScript: local variable and global variable. These variables can be defined using keywords var or let or const.

let

First, we will learn to create variables using the let keyword.

The statement below creates a variable with the name “mssg”. Below we have declared a variable with the name as mssg.

let mssg

Now we can put some data into it by using an assignment operator =

let mssg
mssg="Hello"

This string is stored in the memory area associated with the variable.

We can also combine the declaration and assignment in a single line

let mssg="Hello"

To access the data we call the variable name as follows:

let mssg="Hello";
alert(mssg);

That's all for part 1.

Stay tuned for part 2.In the next part we will see var and const.