Javascript data types and eqality vs identity operators.

JavaScript has dynamic types. This means that the same variable can be used as different types:

var x;               // Now x is undefined
var x = 5;           // Now x is a Number
var x = "John";      // Now x is a String

JavaScript has string type:

var carName = "Volvo XC60";   // Using double quotes
var carName = 'Volvo XC60';   // Using single quotes

JavaScript has only one type of numbers. Numbers can be written with, or without decimals:

var x1 = 34.00;   // Written with decimals
var x2 = 34;      // Written without decimals

JavaScript has booleans. Booleans can only have two values: true or false.

var x = true;
var y = false;

JavaScript has arrays:

var cars = ["Saab", "Volvo", "BMW"];

JavaScript has objects. Object properties are written as name:value pairs, separated by commas.:

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

Undefined and Null
The value of a variable with no value is undefined.
Variables can be emptied by setting the value to null.

var cars;              // Value is undefined
var person = null;     // Value is null

The typeof Operator
You can use the JavaScript typeof operator to find the type of a JavaScript variable.

typeof "John"                 // Returns string 
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object
typeof {name:'John', age:34}  // Returns object

Equality (==, !=)

- If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.

- NaN(not a number, 0/0) is not equal to anything including itself.

- Negative zero equals positive zero.

- null equals both null and undefined.

- Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.

- Every other comparison is considered unequal.

Identity (===. !==)

These operators behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal.

Used following resources: js_datatypes  and Js comparison operators.

Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this