Variables in JavaScript
Posted by Superadmin on May 01 2023 06:40:13

Variables in JavaScript

By Priya PedamkarPriya Pedamkar
  

Variables in JavaScript

Introduction to Variables in JavaScript

Before starting with the JavaScript variable we should know what the term actually means. In general, variable means something which is kept on changing. In programming terms, the variable is nothing but the reference to the memory in which we have stored some value. We have three different ways to declare the variable currently. Each one has its own importance to use. Those are as follows:

  1. var
  2. let
  3. const

All the above three are keywords in JavaScript. In general, we have to declare a variable with the keyword var. Later in ES2015 (EcmaScript 2015 ) has introduced two new keywords for declaring variables i.e. let and const. These both are block-scoped. But for now, we are going to concentrate on var.

Syntax for Variables in JavaScript

The javascript variable comes with the scope. JavaSript has two scopes local scope and global scope. Later in ES2015, we introduced with block scope.

Let’s concentrate on a global scope and local scope. Any variable declared on the top of the code is nothing but global to all. Anyone can access it. In other side variable declared inside any function is local to that function and it cannot be accessed from outside that scope.

Example #1

Code:

var color = "black";
function car(){
var model = "suzuki";
console.log("The model is "+model);
}
console.log("The color is: " + color);
car();

Output:

Variables in JavaScript output1

In the above example, variable color is a global variable and the variable model is a local variable. The variable model is local to its functional scope. And it is not accessible from outside. If we try to do so then we will get an error. Let’s check it out.

Example #2

Code:

var color = "black";
function car(){
var model = "suzuki";
console.log("The model is "+model);
}
console.log("The color is: " + color);
car();
console.log(model);

Output:

Variables in JavaScript output2

Syntax for Respective Declaration

Following syntax are for respective declaration:

Rules For Javascript Variables

Below is the rule for the javascript variable:

  1. Javascript variable name has to start with the letter.range is a-z and A-Z.
  2. It may start with $ and _.
  3. Javascript variables should not start with a digit.
  4. We can use digit in between the name. ex: house1
  5. JavaScript is case sensitive So variable declared as price and Price are different.
  6. JavaScript variable names should not have the name or keywords. Ex: variable const; this is not allowed as const is a keyword.

How Do Variables work in JavaScript?

JavaScript variables are scopes based. Let’s understand first what is scope in JavaScript. JavaScript works on its lexical environment. Some people find it tricky, but it is straightforward:

Code:

var  price;

Code:

var price =10; //initializing variable with value of 10

Code:

var price;
console.log (price); //printing value to the console

In JavaScript, we are showing output on console with console.log method as shown above.

Output:

Variables in JavaScript output3

Do remember that JavaScript is case sensitive in nature. Now, Let’s see if we assign value to the variable then what will the output.

Code:

var price =10; // initialized variable with the value 10
console.log(price);

Output:

output4

Declaring variable with the keyword var are belong to its global scope by default. We can declare may variables on the same line. For that, we need to separate each variable with a comma(,).

Also, the Key to note down here is the value of the variable which is numeric should not be given in quotes. If you give the number in quotes then JavaScript this as a string.

Examples in Variables in JavaScript

Below is some example of variables in javascript:

Example #1

JavaScript var can store data irrespective of its type. Let’s check one example on this.

Code:

var color = "black";
var car = "BMW";
var no = 10;
console.log("I have " + car +
" which is having "+ color+
" color, and having parked at no " +no);

Output:

output5

Example #2

We can declare above all variables in the same line, Look at the below code.

Code:

var color = "black", car = "BMW", no = 10;
console.log("I have " + car +
" which is having "+ color+
" color, and having parked at no " +no);

Output:

output6

We just separated each variable by comma and at the same time, we initialized that.

Example #3

Now, we have looked at declaring variables once. What happens if we re-declare the variables? Look at the below example.

Code:

var color = "black";
var color;
console.log("The color is: " + color);

Output:

output7

Even if we are re-declaring the variable called color here, it is not losing its value. It gives us black as an output.

Conclusion

JavaScript is interpreted language and dynamic in nature. So understanding how it works is very important. Scoping of the variable is a very important aspect while learning JavaScript.