Click here to Skip to main content
15,881,709 members
Articles / Web Development / HTML
Tip/Trick

Javascript Operators === & !==

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
28 Feb 2011CPOL 19.9K   5   6
Javascript Operators === & !==
The following example will demonstrate the Tip:
var x;
x = 0;
var y;
y = '0';

x is numeric, while y is char.

On comparing them using the "==" operator, it will give true !
alert('x == y : ' + (x==y));


To do so, we will use the '===' operator:
alert('x === y : ' + (x===y));

which checks on the Value and Type, so it will give false.

Same for the !== operator.
alert('x != y: '; + (x != y));

it gives false !!
alert('x !== y: '; + (x !== y));

it gives true


So, to assure an accurate comparison datatype and value don't use the normal operators == and != but use === and !== operators.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Egypt Egypt
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

Comments and Discussions

 
GeneralReason for my vote of 3 Nice tip to take care while using sc... Pin
r verma23-Feb-11 16:46
r verma23-Feb-11 16:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.