Learning JavaScript tutorial – Understanding JavaScript Variable Types

Learning JavaScript tutorial – Understanding JavaScript Variable Types

JavaScript Variable is the best mean of storing the data (value). The JavaScript is a loosely typed programming languages and do not need to define the type of a variable while defining it. The main advantage of JavaScript Variable is that it can held any kind of data values, or types, which include number, string, Boolean, and null.

JavaScript allows us to assigned any variable with any value. This feature of javascript provides great flexibility in coding. In this article we are going to Understand the JavaScript Variable Types. If you want to learn some basic about the JavaScript Variables please go through "Learning JavaScript tutorial – JavaScript Variables".

JavaScript Number Variable

A number is a mathematical object which is used for counting and measuring. Number are of different types like Natural, Integers, Rational, Real and Complex. While defining a JavaScript Variable of Number Types we do not need to specify its type as an integers, floating-point (decimal) numbers, or any other number type.

var intNum=10; //Number variable declared with integers values
var floatNum=10.7; //Number variable declared with decimal values
var negativeNum=-13; //Number variable declared with negative integers values

JavaScript String Variable

String is nothing more than a sequence of characters. String variables are declared to store a string data type which may be some characters, letters, words, any number, white spaces, special symbols, or anything you like to put in between two double quotes("") or single quotes (”).

var empName="Ramakant"; //String variable declared
var empAddress="9th Floor, Green Heritage, Navi Mumbai, 410210";
var empPAN=‘AKFPA97C’;
var empSystem="P4 2Ghz, 4GB RAM, WinXP";
var empEmail="ramakant@example.com";

JavaScript string variables are case sensitive which means "Ramakant" is not equal(same) to "ramakant". JavaScript string variables also support the use of Special characters like Tab(\t) and New Line(\n).

var empAddress="9th Floor,\tGreen Heritage,\nNavi Mumbai,\t410210";

Following table show the list of Special JavaScript Characters supported by JavaScript String variables.

JavaScript Special Characters
Output Character Code to Use
Backslash (\) \\
Double quote (") \"
Single quote (‘) \’
Backspace \b
Form feed \f
Newline \n
Carriage return \r
Tab \t
Vertical Tab \v

JavaScript Boolean Variable

JavaScript Boolean variables are used to store logical data type like true or false; 0 or 1. Instead of using the words true and false, JavaScript also permit the use the number 1 for true and 0 for false. JavaScript Boolean variables plays a vital role while working on some conditional expression.

var isRamakantBoy=true;
var isRamakantGirl=flase;
var isRamakantBoy=1;
var isRamakantGirl=0;

While assigning value to a JavaScript Boolean variable we do not need to be enclosed true or false in quotes. If you enclosed true or false in the quote it will become a JavaScript string variable instead of JavaScript Boolean variable.

var isRamakantBoy=true; // JavaScript Boolean variable
var isRamakantBoy=‘true’; //JavaScript string variable

JavaScript NULL Variable

Null means "nothing" or without value or consequence. It is not a white space, nor is it a zero; it simply represent nothing.

var aNullValue=null;


Leave a Reply

Your email address will not be published. Required fields are marked *