{"id":11686,"date":"2014-02-05T18:34:11","date_gmt":"2014-02-05T13:04:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11686"},"modified":"2016-12-19T15:31:05","modified_gmt":"2016-12-19T10:01:05","slug":"equality-vs-identity","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/equality-vs-identity\/","title":{"rendered":"Equality(==) vs Identity(===)"},"content":{"rendered":"<p>In <strong>JavaScript<\/strong>, we have <strong>Equality (==)<\/strong> and <strong>Identity(===)<\/strong> operators for equality comparison between two values\/operands. When first time we see <strong>Equality (==)<\/strong> and <strong>Identity (===) <\/strong>operators, then following questions arise in our mind.<br \/>\n1. What is difference between <strong>Equality (==)<\/strong> and <strong>Identity (===) <\/strong>?<br \/>\n2. Is there a performance benefit with <strong>Equality (==)<\/strong> or <strong>Identity (===)<\/strong> ?<\/p>\n<p>Answers:<br \/>\n1. What is Difference Between <strong>Equality (==)<\/strong> And <strong>Identity (===)<\/strong> ?<br \/>\n<strong>Equality (==)<\/strong> returns true if both value\/operands have the same value, without worrying about type of both value\/operands.<br \/>\ne.g.<br \/>\n[js]<br \/>\n2 == 1 \/\/ returns false, both value\/operands have different value.<br \/>\n2 == 2 \/\/ returns true, both value\/operands have same type and value<br \/>\n2 == &quot;2&quot; \/\/ returns true, while both value\/operands have different type, first value is type of number and second one is type of string, but value is same.<br \/>\n[\/js]<br \/>\n<strong>Equality (==) <\/strong>first compares types, if types are not same, then first convert both values\/operands to same type, then compare both value\/operands, while on other hand <strong>Identity (===) <\/strong>first compares types, if types are same then it only compares the value of both the operands, otherwise returns false.<br \/>\ne.g.<br \/>\n[js]<br \/>\n2 === 1 \/\/ returns false, both value\/operands have different value.<br \/>\n2 === 2 \/\/ returns true, both value\/operands have same type and value<br \/>\n2 === &quot;2&quot; \/\/ returns false, both value\/operands have same value but different types, first value is type of number and second one is string.<br \/>\n[\/js]<\/p>\n<p>2. Is there a performance benefit with <strong>Equality (==)<\/strong> or <strong>Identity (===)<\/strong> ?<br \/>\n<strong>Identity (===) <\/strong>does not do type conversion (if types are not same it returns false), So <strong>Identity (===) <\/strong>is faster than <strong>Equality (==)<\/strong>.<\/p>\n<p>NOTE: <strong>Identity (===)<\/strong>, is generally better and safer, because there is no behind the scenes type conversion.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, we have Equality (==) and Identity(===) operators for equality comparison between two values\/operands. When first time we see Equality (==) and Identity (===) operators, then following questions arise in our mind. 1. What is difference between Equality (==) and Identity (===) ? 2. Is there a performance benefit with Equality (==) or Identity [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1185],"tags":[1313,1312,55,1308],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11686"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11686"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11686\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}