JS variables -var,let,const

Nanduni Weerasinghe
3 min readApr 7, 2021

--

Like in other languages, JS also has variables and uses var, let and sometimes use const to declare a variable. Where can we exactly use these keywords? and how? Confused right?. Let’s learn about the variables in JavaScript today, and this page will give you an idea about how to declare variables in JavaScript and where to use them. 😄

So, Mainly in JS, We can declare a variable in three ways,

  1. using the var keyword,
  2. using the let keyword,
  3. using const keyword.

Before using these, let's learn the rules of declaring a variable in JS.

  • A variable name should not contain any white spaces, can not start using a number, and cannot use keywords as variable names.
  • Variable names can write using letters(A-Z), numbers(1–9) , Dollar sign($) and underscore( _ ).
  • Variable names are case-sensitive.

Now you know the rules. So, let’s use them and declare a variable.😃

  1. using the var keyword,
// name a variable using var

var name;
var student_name;
var Age01;

In the same way, we can declare variables using let and const as well.

2. using the let keyword,

// name a variable let keyword
let name;
let Number02;
let FullName;

3. using const keyword.

// name a variable const keyword
const name;
const
Number02;
const
FullName;

As you can see here, in JS, we don't have to mention the data type. which means they are loosely typed variables.

let, const, or var?

There are some differences in variables, which we have declared using these keywords. In this section, we will learn about them.

1.Redeclaring a variable

Though we can redeclare a variable using var, using let and const keywords we can not do so. (that means you can only use the var keyword for redeclaring.)

//redeclaring a varible 
var name ="tree";
var name = "flower";
console.log(name);
// output : flower

2. Updating

(Here, updating means reassigning values)Variables that are declared using let and var can be updated. But variables that used const keyword to declare cannot be updated.

//updating variable using var
var name = "tree";
name = "flower";

console.log(name); // output : flower
//updating variale using let
let name = "tree";
name = "flower";

console.log(name); // output : flower

3. Scope

The scope of a variable defines from where we can access them. In JS, we have global scope (declared outside of the block)and local scope(declared inside the block).

Local scope variables can only use inside that block. The keyword let gives a functional scope or local scope to the variable and const does the same.


// block scope of let
function sum()
{
let number_01 = 1;
let number_02 = 2;
return number_01+number_02; //returns the total of number_01 and number_02
}
sum(); //calling the functionconsole.log(number_01);// reference error
const number_01= 3;
console.log(number_01);
// output is 3

Since variables which use const cannot be updated or reassigned we can use them to represent constant values or particular value that doesn't change time to time in our code.

// block scope of const function sum (){
const n1 = 1;
const n2 = 2;
return n2+ n1;
//returns the total of n1 and n2
}
console.log(n1);// reference errorconst n1 = 3;
console.log(n1);
// output is 3

Even though let and const are block-scoped, the keyword var gives both global and block scope to a variable.

// block scope and global of var
var n = 1;
if (n=== 1 )
{
n = 2;
console.log(n);
//output is 2
}console.log(n);//output is 2

So, now you know about the JS variables and the var, let , const keywords.

We also learned about the declaration rules of variables.

Let’s meet again in another article. ❤️

I will stop by here. 😄

Thank you for reading. 😍

--

--