Variables are used to store data. It is convenient to use variables instead of the actual data. The data stored in a variable can be changed after it was initially assigned, hence the name "variable".

There are two steps required in order to use a variable.

You need to

  1. Declare the variable
  2. Initialize it. (ie., give it a value)
Declare Variable

In order to declare a variable, you use the var statement, like this

var a; //a single letter
var thisIsAVariable; //it can have numbers
var _and_this_too; //it can have underscore
var start12three; //it can have numbers
You cannot specify the data type of a variable. Javascript assigns to the variable, the data type of the value you assign. A variable doesn't not have a fixed type. You can assign any value to the variable.

For the names of the variables, you can use any combination of letters, numbers, and the underscore character. However, you can't start with a number, which means that this is invalid.

var 2three4five; //invalid
Initialize Variable

To initialize a variable means to give it a value for the first (initial) time. You have two ways to do so.

  1. Declare the variable first, then initialize it, or
  2. Declare and initialize with a single statement

An example of the latter is:

var a = 1;

Now the variable named a contains the value 1.

Initialize Multiple Variable

You can declare and optionally initialize. several variables with a single var statement. Just separate the declarations with a comma.

var v1, v2, v3 = 'hello', v4 = 4, v5;

You can initialize a variable without assigning a value to it. To do this you must attribute the null value.

var myVar1 = null;
var myVar2 = 2;
var result = myVar1 * myVar2; //output -> 0

The value of the above result is 0. This is because, during the multiplication, the interpreter will convert the myVar1 value to a Number and will assign the value 0 by default.

You can also declare a variable without initializing it. In this case, the variable exists, but its value is undefined.

var myVar1, myVar2 = 2;
var result = myVar1 * myVar2;

The value of myVar1 is undefined. Consequently, when the interpreter runs the multiplication, it produces the value NaN(Not a Number), and assigns this value to result.

Variables are Case Sensitive

Variable names are case-sensitive. The below variable names case_matters and CASE_MATTERS are independent and different.

var case_matters = 'lower';
var CASE_MATTERS = 'upper';
Scope of variables

You must be careful where you declare a variable and how you declare a variable whether your variable want to have local scope or global scope.

Local scope:

If you declare your variable either in a function or in a block or in a control structure then your variable will have a local scope. This means that the scope of your variable will be local to the block which you declared. This rule applies only if you declare your variable using the var keyword. If you dont use the var keyword then your variable will have global scope.

Global scope:

If you declare your variable outside of any function or block or control structure, then your variable will have global scope. This means that the scope of your variable will be global over all the javascript code.

Related Tutorial
Follow Us #
Contents +