Javascript Variables

Javascript variables work as data containers. Means, variables store and sort a number of data types for calculation and to use the data in the future when the data is required, simply by calling the variable name.


var mgs = "Hello, World!"; //stored text message in "mgs" variable
console.log(mgs) //called the varibale by its name to get the stored value

//expected result: Hello, World!

Javascript Variable

Javascript variables are constructed with the var keyword, variable name, the assignment operator and the variable value.

Javascript Variables

You can declare a Javascript variable with or without variable value. After declaring a variable with the var keyword, later you can assign the value using varName = varValue syntax.

Calling a variable that does not contain a value will output undefined

Initially to declare a variable you must have to use the var keyword.

You can declare Javascript Variables by following ways:


var var1; //declare a single variable without assigning a value
var1 = 10; //Assigned value as 10 to the var1 variable.
var var1 = 10; //declare a single variable with assigning a value
// both are same
var var1, var2, var3; //declare multiple variable without assigning values
var1 = 10;
var2 = 12;
var3 = var2 - var1; //assigned values separately
//it also can be
var var1 = 10, var2 = 12, var3 = var2 - var1;

Type of values in JS variable

You can declare total of five types of variable in Javascript. means, Javascript can contain five types of data called data types. Ideally, they are:

String Data

In general, strings are text data. In Javascript, any text data or string can be stored in variables inside the double quotation ( " " ) or a single quotation ( ' ' ).


var myString = "Hello, World!"; //String data with double quotation
var myString = 'Hello, World!'; //String Data with double quotation

Number Data

In Javascript, number data or integer value can be stored in a variable using var myNumber = integervalue; syntax. Note that, while assigning an integer value to a variable, you don’t have to use single or double quotation like string.


var myNumber = 10;
//don't mess up by putting quotation while declaring integer or number data

Boolean Data

Booleans data refer to the logic true and false. This kind of data or logic repeatedly used in conditional statement. Boolean can be declared in variable like below:


var myLogic = true; //Don't need to put a quotation
var myLogic = false; 

Array Data

In Javascript, array refers to a structure that can contains multiple value. You can assign array value in a variable by following way:

The array is an advanced concept in Javascript. We will learn in details about Array in later chapters


var myArray = [1, 'Mike', 'Alex', 10];
//This array contain two numbers and two string values Mike and Alex.
//Notice that, string or text value uses quotation but number does not

Object Data

In Javascript, anything can be considered as an object. And an object can be stored in a variable. Means, you can store anything in a variable. See the following example,


document.querySelector('p'); //It is an object, let's assign it to a variable
var myObject = document.querySelector('p');
//In this way, we can assign any statement in a variable

« Previous Next »

Posted in