Show
In Javascript, we have couple of options for checking equality:
In this post, we’ll explore the similarities and differences between these operators. Let’s declare two variables
In the above example, both operators returned the same answer i.e. The Difference between == and ===The difference between
Let’s take a look at another example:
Is === Faster than ==? A Quick Look at the Performance of the Two OperatorsIn theory, when comparing variables with identical types, the performance should be similar across both operators because they use the same algorithm. When the types are different, triple equals operator (
If you look at the graph at the bottom of the tests, you’d see performance varies across different browser implementations and the gains in performance are almost negligible. But if you think about it, performance is totally irrelevant and shouldn’t play a role in deciding when to use
one operator over the other. Either you need type coercion or you don’t. If you don’t need it, don’t use double equals operator ( In short, always use Inequality Operators: != and !==
Equality Operators and Objects (and other reference types)So far, we have been exploring equality or inequality operators using primitive types. What about reference types like Arrays or Objects. If we create two arrays that have identical contents, can we compare them using equalty operators the same way we do it for primitives? The answer is no, you can’t. Let’s take a look at an example:
Here, both the ECMAScript 6: Object.is()I said that the beginning of the article that are couple of options for checking equality in Javascript. That isn’t true anymore. ECMA Script 6 introduced a third method for comparing values:
Triple equals operator (
To make comparisons less confusing, ECMAScript 6 introduced a new method: Object.is(). It takes two arguments and returns
Next up, read our Free Primer on JavaScript. If you like this post, please share using the buttons above. It will help CodeAhoy grow and add new content. Thank you! What is the use of == and === 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 does JavaScript 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 ...
What is the purpose of === in JavaScript?The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
What does == === mean?The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.
|