JavaScript Variables and Types

Every language has a basic and important thing is Variables and Types. We always require to design some variables or types to store and process some values. Like all other languages JavaScript has also some variable types. But in JavaScript we can declare all the types of variable using 'var' keyword. It can be used to store all the types of values into the variables. Like we can store integer, string, boolean and decimal etc.

We can define various types of variable using keyword var as below.

Example
var intAge = 10;                   	// full number example
var decimalMoney = 23.45;                   // decimal number example
var stringFirstName = "VK Tutorials"        // string example
var boolIsDay = true;               		// boolean example

Some notes about the JavaScript types.

In boolean type it can hold either true or false value. In number it can hold integer number or decimal number.

There are some other advanced types as well in the JavaScript like Object and Array. We will see the basic details in this lession and later we will see array in more detail in other lession.

Example
var employee = {}; 	
// an example of object, here we can store employee name, DOB, Address etc.
var employees = [];	
// an example of array type, in this we can store detail of all the employees.

Now we will see if we are not assiging any value to them then what will happen. If we are declaring a varialbe and not assiging any value to it and using that variable then it will have a value as undefined. Undefined is also counted as one of the types in JavaScript. Below example shows how any variable has undefined value.

Example
 var firstName;
 console.log(firstName);	// This will print undefined on the console.

If we want to declare any variable and don't want to assign any default value then null can be used to initialize the variable. Below example will illustrate how we can initialize any variable with null value.

Example
 var firstName = null; 
 console.log(firstName); // This will print null on the console.

You can now practice for defining some variables and use those variables value into the JavaScript code to understand better how it works.

 

← Previous

Next →