Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Object is() Method

JavaScript Object is() Method

The object.is() method is used to determine whether two values are the same or not. This Object.is() takes two arguments which are the values to be compared and returns a boolean indicating whether the two arguments are the same or not.

Syntax:

Object.is(value1, value2)

Parameters:

  • value1: It is the first value to be compared. 
  • value2: It is the second value to be compared.

Return Value: Object.is() returns a boolean indicating whether the two arguments are the same or not. 

Examples of the above function are provided below.

Example 1: In this example, the Object.is() method returns true because both the strings passed as an argument are of the same length with the same characters and in the same order. 

javascript




// Comparing strings of the same length 
// with the same characters in the same order  
console.log(Object.is('neveropen', 'neveropen'));


Output: 

true

Example 2:  In this example, the Object.is() method returns false because both the strings passed as an argument are not of the same length with the same characters and in the same order. 

javascript




// Comparing two different strings
console.log(Object.is('neveropen', 'gfg'));


Output: 

false

Example 3: In this example, the Object.is() method returns false because both the numbers passed as an argument are of different polarity. 

javascript




// Comparing 0 and -0 
console.log(Object.is(0, -0));


Output: 

false

Example 4: In this example, an object “check” has been created and passed as a parameter to the Object.is() method. It returns true because both the parameters are a single object and are compared with each other.

javascript




// Comparing a variable with itself
let check = { a: 100 };
console.log(Object.is(check, check));


Output: 

true

Applications:  

  • Object.is() is used for the comparison of two strings, numbers,objects.

Exceptions:

  • The “==” and “===” operator treats the number values “+0” and “-0” as equal but the object.is() method treats them differently.
  • The Object.is() method does not coerce values before comparison even if they are of different data types.

Two values can be the same if they hold one of the following properties: 

  • If both the values are undefined.
  • If both the values are null.
  • If both the values are true or false.
  • If both the strings are of the same length with the same characters and in the same order.
  • If both the values are numbers and both are “+0” or both are ‘-0’.
  • If both the values are numbers and both are “NaN” or both non-zero and both not NaN and both have the same value. 

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browser:

  • Chrome 30 and above
  • Edge12 and above
  • Firefox 22 and above
  • Opera17 and above
  • Safari 9 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments