Should i use == or === in javascript?
The strict equality operator ( Show
Reference: Javascript Tutorial: Comparison Operators The To quote Douglas Crockford's excellent JavaScript: The Good Parts,
Update:A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning objects. For objects,
The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its
Here the Reference answered Dec 11, 2008 at 14:25
Bill the LizardBill the Lizard 390k207 gold badges558 silver badges865 bronze badges 6 Using the
Using the
This is because the equality operator On the other hand, the identity operator 1 Here's an interesting visualisation of the equality comparison between Source: https://github.com/dorey/JavaScript-Equality-Table (demo, unified demo) var1 === var2When using var1 == var2When using Summary of equality in JavascriptConclusion:Always use
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered May 5, 2014 at 5:21
SNagSNag 17k10 gold badges48 silver badges65 bronze badges 8 In the answers here, I didn't read anything about what equal means. Some will say that So, let's take the following code:
The same here:
Or even:
This behavior is not always obvious. There's more to the story than being equal and being of the same type. The rule is: For value types (numbers): For reference types: For strings: Strings: the special case...Strings are not value types, but in Javascript they behave like value types, so they will be "equal" when the characters in the string are the same and when they are of the same length (as explained in the third rule) Now it becomes interesting:
But how about this?:
I thought strings behave like value types? Well, it depends who you ask... In this case a and b are not the same type.
answered Jun 5, 2009 at 19:11
Philippe LeybaertPhilippe Leybaert 165k31 gold badges210 silver badges221 bronze badges 0 Let me add this counsel: If in doubt, read the specification! ECMA-262 is the specification for a scripting language of which JavaScript is a dialect. Of course in practice it matters more how the most important browsers behave than an esoteric definition of how something is supposed to be handled. But it is helpful to understand why new String("a") !== "a". Please let me explain how to read the specification to clarify this question. I see that in this very old topic nobody had an answer for the very strange effect. So, if you can read a specification, this will help you in your profession tremendously. It is an acquired skill. So, let's continue. Searching the PDF file for === brings me to page 56 of the specification: 11.9.4. The Strict Equals Operator ( === ), and after wading through the specificationalese I find:
Interesting is step 11. Yes, strings are treated as value types. But this does not explain why new String("a") !== "a". Do we have a browser not conforming to ECMA-262? Not so fast! Let's check the types of the operands. Try it out for yourself by wrapping them in typeof(). I find that new String("a") is an object, and step 1 is used: return false if the types are different. If you wonder why new String("a") does not return a string, how about some exercise reading a specification? Have fun! Aidiakapi wrote this in a comment below:
new always returns an Object, even for String constructors, too. And alas! The value semantics for strings (see step 11) is lost. And this finally means: new String("a") !== "a".
answered Nov 28, 2009 at 18:18
nalplynalply 24.6k12 gold badges79 silver badges96 bronze badges I tested this in Firefox with Firebug using code like this:
and
My results (tested five times each and averaged):
So I'd say that the miniscule difference (this is over 100000 iterations, remember) is negligible. Performance isn't a reason to do
kabirbaidhya 3,0543 gold badges33 silver badges53 bronze badges answered Dec 25, 2008 at 11:17
Simon ScarfeSimon Scarfe 9,1304 gold badges27 silver badges32 bronze badges 3 In PHP and JavaScript, it is a strict equality operator. Which means, it will compare both type and values. answered May 12, 2010 at 12:58
ShikiShiki 16.6k7 gold badges31 silver badges33 bronze badges In JavaScript it means of the same value and type. For example,
but
answered May 12, 2010 at 12:58
DimitarDimitar 2,3722 gold badges19 silver badges28 bronze badges 0 Why What
do you get when you compare an empty string
Yep, that's right according to And it doesn't end there, here's another one:
Things get really weird with arrays.
Then weirder with strings
It get's worse: When is equal not equal?
Let me say that again:
And this is just the crazy stuff you get with primitives. It's a whole new level of crazy when you use At this point your probably wondering... Why does this happen? Well it's because unlike "triple equals" (
It has special handling for functions, special handling for nulls, undefined, strings, you name it. It get's pretty wacky. In fact, if you tried to write a function that does what
So what does this mean? It means Because it's complicated it's hard to know what's going to happen when you use it. Which means you could end up with bugs. So the moral of the story is... Make your life less complicated. Use The End. answered Aug 9, 2016 at 16:50
Luis PerezLuis Perez 27.1k10 gold badges77 silver badges80 bronze badges 1 The === operator is called a strict comparison operator, it does differ from the == operator. Lets take 2 vars a and b. For "a == b" to evaluate to true a and b need to be the same value. In the case of "a === b" a and b must be the same value and also the same type for it to evaluate to true. Take the following example
In summary; using the == operator might evaluate to true in situations where you do not want it to so using the === operator would be safer. In the 90% usage scenario it won't matter which one you use, but it is handy to know the difference when you get some unexpected behaviour one day. answered Dec 11, 2008 at 14:58
Doctor JonesDoctor Jones 20.7k12 gold badges75 silver badges98 bronze badges 0
=== checks same sides are equal in type as well as value.Example:
Common example:
Another common example:
Many times an untyped check would be handy because you do not care if the value is either
answered May 12, 2010 at 12:58
vsyncvsync 107k53 gold badges285 silver badges370 bronze badges 0 Javascript execution flow diagram for strict equality / Comparison '===' Javascript execution flow diagram for non strict equality / comparison '==' answered Sep 5, 2015 at 13:53
Samar PandaSamar Panda 3,9963 gold badges24 silver badges32 bronze badges 0 JavaScript
answered Jul 3, 2013 at 4:08 0 It means equality without type coercion type coercion means JavaScript do not automatically convert any other data types to string data types
answered May 12, 2010 at 12:59
Pop CatalinPop Catalin 60.2k22 gold badges88 silver badges113 bronze badges 0 In a typical script there will be no performance difference. More important may be the fact that thousand "===" is 1 KB heavier than thousand "==" :) JavaScript profilers can tell you if there is a performance difference in your case. But personally I would do what JSLint suggests. This recommendation is there not because of performance issues, but because type coercion means
answered Dec 16, 2008 at 14:29
ConstantinConstantin 26.9k10 gold badges58 silver badges79 bronze badges 0 The equal comparison operator == is confusing and should be avoided. If you HAVE TO live with it, then remember the following 3 things:
EQUAL OPERATOR TRUTH TABLE IN JAVASCRIPT
** STRANGE: note that any two values on the first column are not equal in that sense.**
answered Sep 16, 2011 at 14:25
CuongHuyToCuongHuyTo 1,28313 silver badges19 bronze badges 0 There is unlikely to be any performance difference between the two operations in your usage. There is no type-conversion to be done because both parameters are already the same type. Both operations will have a type comparison followed by a value comparison. answered Dec 11, 2008 at 14:44
SeanSean 2,2051 gold badge23 silver badges31 bronze badges 0 Yes! It does matter.
You can easily test it. Paste following code in an HTML file and open it in browser
You will get 'false' in alert. Now modify the answered Nov 14, 2014 at 6:02
Aniket ThakurAniket Thakur 64.4k37 gold badges267 silver badges284 bronze badges 0 Simply
and
Type coercion in JavaScript means automatically converting data types to other data types. For example:
answered Mar 20, 2015 at 5:05
AmitAmit 7979 silver badges15 bronze badges As a rule of thumb, I would generally use Reasons are explained in in the answers above and also Douglas Crockford is pretty clear about it (JavaScript: The Good Parts). However there is one single
exception:
For example jQuery 1.9.1 uses this pattern 43 times, and the JSHint syntax checker even provides the From the jQuery style guide:
EDIT 2021-03: Nowadays most browsers support the Nullish coalescing operator (
can be written as any of these forms
answered Apr 27, 2013 at 14:15
mar10mar10 13.6k5 gold badges37 silver badges63 bronze badges 1 It's a strict check test. It's a good thing especially if you're checking between 0 and false and null. For example, if you have:
Then:
All returns true and you may not want this. Let's suppose you have a function that can return the 0th index of an array or false on failure. If you check with "==" false, you can get a confusing result. So with the same thing as above, but a strict test:
answered May 12, 2010 at 13:19
DanielDaniel 8806 silver badges5 bronze badges 1
Alex Weitz 2,9804 gold badges32 silver badges50 bronze badges answered May 12, 2010 at 13:03
JSLint sometimes gives you
unrealistic reasons to modify stuff. It is faster only when the types are not the same, in which case it does not try to convert types but directly returns a false. So, IMHO, JSLint maybe used to write new code, but useless over-optimizing should be avoided at all costs. Meaning, there is no reason to change Modifying a lot of code that way wastes developers' and reviewers' time and achieves nothing.
Alex Weitz 2,9804 gold badges32 silver badges50 bronze badges answered Jun 5, 2012 at 7:53
ashesashes 6111 gold badge6 silver badges9 bronze badges 1 A simple example is
answered May 14, 2015 at 14:45
VikasVikas 4,1931 gold badge33 silver badges39 bronze badges The top 2 answers both mentioned == means equality and === means identity. Unfortunately, this statement is incorrect. If both operands of == are objects, then they are compared to see if they are the same object. If both operands point to the same object, then the equal operator returns true. Otherwise, the two are not equal.
In the code above, both == and === get false because a and b are not the same objects. That's to say: if both operands of == are objects, == behaves same as ===, which also means identity. The essential difference of this two operators is about type conversion. == has conversion before it checks equality, but === does not. answered Sep 9, 2013 at 8:31
Harry HeHarry He 1,75515 silver badges12 bronze badges The problem is that you might easily get into trouble since JavaScript have a lot of implicit conversions meaning...
Which pretty soon becomes a problem. The best sample of why implicit conversion is "evil" can be taken from this code in MFC / C++ which actually will compile due to an implicit conversion from CString to HANDLE which is a pointer typedef type...
Which obviously during runtime does very undefined things... Google for implicit conversions in C++ and STL to get some of the arguments against it...
answered Dec 29, 2008 at 11:54
Thomas HansenThomas Hansen 5,5231 gold badge22 silver badges28 bronze badges 1 From the core javascript reference
answered May 12, 2010 at 12:59
Paul ButcherPaul Butcher 6,82427 silver badges39 bronze badges Equality comparison: Operator Returns true, when both operands are equal. The operands are converted to the same type before being compared.
Equality and type comparison: Operator Returns true if both operands are equal and of the same type. It's generally better and safer if you compare this way, because there's no behind-the-scenes type conversions.
answered Oct 2, 2013 at 21:54
user2601995user2601995 6,0758 gold badges36 silver badges41 bronze badges Here is a handy comparison table that shows the conversions that happen and the differences between As the conclusion states:
http://dorey.github.io/JavaScript-Equality-Table/
Bonifacio2 3,0284 gold badges33 silver badges49 bronze badges answered Mar 27, 2014 at 0:18
null and undefined are nothingness, that is,
Here So, the 0, false and '' together form a sub-group. And on other hand, null & undefined form the second sub-group. Check the comparisons in the below image. null and undefined would equal. The other three would equal to each other. But, they all are treated as falsy conditions in JavaScript. This is same as any object (like {}, arrays, etc.), non-empty string & Boolean true are all truthy conditions. But, they are all not equal. Should you use == or === JavaScript?== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.
Should you ever use == in JavaScript?Short answer: never. This post looks at five possible exemptions from the rule to always use === and explains why they aren't. JavaScript has two operators for determining whether two values are equal [1]: The strict equality operator === only considers values equal that have the same type.
Why we use == and === in JavaScript?= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.
Why would you use === instead of ==?Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
|