Click here to Skip to main content
11,638,881 members (75,140 online)
Click here to Skip to main content

Javascript Operators === & !==

, 28 Feb 2011 CPOL 11.4K 5
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Antoun Gorgy
Software Developer
Egypt Egypt
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 4 I like the tip because before knowin... Pin
J walia11-Mar-11 4:46
memberJ walia11-Mar-11 4:46 
GeneralReason for my vote of 4 it is a good tip, and important for ... Pin
egozi1310-Mar-11 9:30
memberegozi1310-Mar-11 9:30 
GeneralReason for my vote of 5 nice tip. Pin
code in play1-Mar-11 7:39
membercode in play1-Mar-11 7:39 
GeneralReason for my vote of 3 Nice tip to take care while using sc... Pin
r verma23-Feb-11 16:49
memberr verma23-Feb-11 16:49 
GeneralReason for my vote of 3 Nice tip to take care while using sc... Pin
r verma23-Feb-11 16:46
memberr verma23-Feb-11 16:46 
GeneralOperator Use == returns true if values o... Pin
r verma23-Feb-11 16:43
memberr verma23-Feb-11 16:43 
Operator Use
== returns true if values of variables are equal
=== returns true if values and variable types are same
!= true if values are not equal
!=== true if values and variable types are not same

These are operators provided in Javascript for comparison. Nice tip to take care while using script.

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 1 Mar 2011
Article Copyright 2011 by Antoun Gorgy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid