Equality in JavaScript

Equality in JavaScript

There are 4 different algorithms JavaScript uses to determine whether two values are equal. Abstract equality, Strict equality, SameValue: Object.is(), SameValueZero. Here's what you need to know.

JavaScript defines 4 different algorithms for determining whether two values are equal:

  1. Abstract equality: ==
  2. Strict equality: ===
  3. SameValue: Object.is()
  4. SameValueZero: Same as Object.is, except -0 is considered equal to +0.

Strict Equality, SameValueZero, SameValue

Strict equality, SameValueZero, and SameValue are almost equivalent. They only differ in their handling of NaN, +0, and -0. For all other values, the last 3 algorithms are identical.

Strict Equality: NaN is not strictly equal to any value, not even itself. In other words, NaN !== NaN. Also, (+0) === (-0).

SameValue: The Object.is() function implements the SameValue algorithm. With the SameValue algorithm, NaN is equal to itself: Object.is(NaN, NaN) === true. But, on the other hand, +0 is not equal to -0: Object.is(+0, -0) === false.

SameValueZero: There's no way to use SameValueZero directly, but the Array#includes()[method](https://masteringjs.io/tutorials/fundamentals/array-includes) uses SameValueZero internally. So, to try out SameValueZero, you can useincludes(). The only difference between SameValue and SameValueZero is that SameValueZero treats +0 as equal to -0: [+0].includes(-0) === true.

As a developer, you should typically use ===, with the understanding that you may need to add a special case if you care about NaN. The distinction between +0 and -0 is not important for most use cases.

Abstract Equality

Abstract equality has numerous differences. The abstract equality algorithm supports several implicit type conversions. Here's a brief overview:

  1. If x and y are the same type, check if x === y.
  2. If x and y are both either null or undefined, return true.
  3. If x is a number and y is a string, convert y to a number and then compare using ===. Similarly, if x is a boolean or string, and y is a number, convert x to a number.
  4. If x or y is a boolean, convert the other value of a number and compare them.
  5. If x is an object and y is a symbol, string, or number, try to convert x to a primitive using valueOf() and then compare using ===.

javascript web-development programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.