My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Declaring Variable in JavaScript

dipak maluskar's photo
dipak maluskar
·Oct 15, 2021·

3 min read

What are Variables?

Variables (or Bindings) are used to name a memory location, inside which some information or some changes done in a program are stored.

Let’s see an example -

var name = prompt("What is your Name?");
\\ user entered "PV"
console.log("Welcome " + name);

In the above example, the prompt stores your data into the variable called “name”.

Note : You can define the variable_name as anything you like.

Variable types -

Var is a keyword used to define a variable. Let’s first understand what a keyword is. Keywords are predefined or reserved words present in the JS. These words already have a defined meaning to them and we CANT use them as variable names! I’ll give you some examples - let, var, const, for, if, break, catch etc. So in order to define a new variable, we need to use one of these three variables - let, var or const.

let first = "Hello";
var second = 2;
const third = 'A';

Note - JavaScript is very liberal in case of defining variables. Unlike other languages, we do not have to specify the data-type of the data that variables will be storing. In short, We do not need to notify JavaScript that we will be storing string or int or char in the variable.

Difference between var, let and const keywords - Let’s start by knowing the usage of and the difference between let and var. You might see in different codes and places, let and var being used interchangeably. They might seem the same to you, but they aren’t. The main difference between let and var is due to the scope of the variable, that the keywords are defining. let is limited to the block in which it is declared while variable declared with var has the global scope. I’m pretty sure you understood nothing by that! Lets dive into an example to understand it.

function textExample(){
    let a = "Prakhar";
    console.log(a);
}
textExample();
console.log(a);

\\ Output of above program is -
\\ Prakhar
\\ Undefined

Here in the above example, we can see that the variable ‘a’ was defined inside the function textExample, and its scope is limited to that function only. When we try to print ‘a’ outside the function, the output is undefined. But that is not the case with var. Let’s see another example -

function textExample(){
    var a = "Prakhar";
    console.log(a);
}
textExample();
console.log(a);

\\ Output of above program is -
\\ Prakhar
\\ Prakhar

Here, unlike the let keyword, the variable is accessible even outside of the function. In this case, the var keyword, has global scope i.e. the scope is not limited to inside the block, it is defined in.

Now let’s move towards const- The const keyword is used to defined constants in a program. Let’s say, you want to define the value of pi, inside your program for some mathematical calculations. In this case you use const.

const pi = 3.1415926;

Now once, you have defined pi constant, it’s value CANNOT be changed under any circumstances inside your program.