Object.is( ) in JavaScript

Object and Object Constructors in JavaScript?
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.

  • Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
  • Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.

For instance, consider the following code:

function Automobile(color) {
  this.color=color;
}

var vehicle1 = new Automobile ("red");

The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”.



When new Automobile() is called, JavaScript does two things:

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.is() Method
Object.is() method is used to determine whether two values are the same or not.
Two values can be 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”.
  • If both the values are numbers and 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.

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

Difference between Object.is() method and “==” operator
The “==” and “===” operator treats the number values “+0” and “-0” as equal whereas Object.is() method treats them as not equal. Apart from that the “==” and “===” operator does not treat Number.Nan equal to Nan.

Applications:

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

Syntax:

Object.is(value1, value2)

Parameters Used:
value1 : It is the first value to be compared.
value1 : It is the second value to be compared.

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

Examples of the above function are provided below.

Examples:



Input : Object.is('geeksforgeeks', 'geeksforgeeks');
Output: true

Explanation : In the above 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.

Input : Object.is('geeksforgeeks', 'gfg');
Output : false

Explanation : In the above 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.

Input : Object.is(0,-0);
Output: false

Explanation : In the above example the Object.is() method returns false because both the numbers passed as an argument are of different polarity.

Input : var check = { a: 100 };
        Object.is(check, check);  
Output: true

Explanation : In the above 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.

Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Comparing strings of the same length 
// with the same characters in the same order  
Object.is('geeksforgeeks', 'geeksforgeeks');
  
</script>

chevron_right


OUTPUT :

true

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Comapring two different strings
Object.is('geeksforgeeks', 'gfg');
  
</script>

chevron_right


OUTPUT :

false
Code 3:


filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Comapring 0 and -0 
Object.is(0,-0);
  
</script>

chevron_right


OUTPUT :

false

Code 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Comparing a variable with itself
var check = { a: 100 };
Object.is(check, check);  
  
</script>

chevron_right


OUTPUT :

false

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.

Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2