JavaScript method | Explained

In JavaScript, the “” method is primarily used to validate the equality of two values through comparison. These values can be string, float, decimal, or integer type. Moreover, the “” method also provides the functionality to check the polarity of two numbers.

Do not confuse the “” method with the JavaScript equality “==” operator because there exist significant differences between them. For instance, the “” is a JavaScript method that is utilized for performing comparison based on “original” values of primitive data types, and the “==” operator compares specified values after converting them to a common type. Secondly, the equality operator marks the numbers “-1” and “1” as equal, whereas the method treats them according to their polarity.

This write-up will explain the method and its usage in JavaScript. So, let’s start!

How to use JavaScript method

JavaScript method is invoked in the following use cases:

  • To compare two numbers.
  • To compare two strings.
  • To compare two objects.
  • To compare the polarity of two numbers.

We will discuss the mentioned use cases of the method in the next section, but before jumping into it, check out the syntax of the method in JavaScript.

Syntax of using JavaScript method, value)

Here, “value1” represents the value that needs to be compared to “value2”:

How to use JavaScript method for comparing strings

The JavaScript “” method can be used for comparing strings. For this purpose, you have to pass both strings as arguments to the method in the following way:

console.log("linuxhint", "linuxhint"));

The above-given “” method will compare the first “linuxhint” string to the second “linuxhint” string, in terms of “length”, “characters,” and the “order” in which characters are assembled.

In our case, both values are equal according to the mentioned criteria, so the return case of the “” method will be set to “true”:

Now, let’s change the second argument value to “linux” and check out the output of the method:

console.log("linuxhint", "linux"));

As the length of the specified string arguments is not the same, method will not further compare them and returns a “false” value:

How to use JavaScript method for comparing objects

In your program, you can also utilize “” method for to perform a comparison between objects. For instance, the below-given method will compare two empty objects:

console.log({}, {}));


At this point, you must be questioning yourself that the passed objects are empty, neither of them has any key-value pair, then why “” returned “false”?

The specified objects look the same; however, they are two different objects as they refer to different memory addresses. This is why the “” method marked these empty objects as unequal after comparing their references.

Also, it does not matter if the added “key-value” pairs are the same in both objects. The JavaScript “” method will still return “false” after execution :

let object1 = { age: 23 };

let object2 = { age: 23 };

console.log(, object2));


Two objects are only considered “equal” if they point towards the same memory address. For example, when we will compare the created “object1” to itself, the “” set “true” as its return case:

console.log(, object1));


How to use JavaScript method for comparing polarity of two numbers

The polarity of a number signifies whether the number is positive or negative. Want to compare the polarity of two numbers in JavaScript? Utilize the “” method in your code and specify numbers as arguments in it.

For instance, the numbers “-1” and “1” passed to the method are not equal as “-1” is smaller than “1”, so the “” method will return “false”:

console.log(, 1));


If the specified numbers are equal in terms of polarity, then in the next step, the “” method will compare them and return “true” if their values are equal.

For instance, both of the arguments in the below-given “” method are “positive,” and their values are equal, so the resultant boolean value will be printed out as “true”:

console.log(, 3));


We have compiled all of the essential information related to the JavaScript method. You can further explore it according to your requirements.


JavaScript method is used to compare two values. It treats objects and primitive values differently. In the case of primitive values, method check them “by-value” and compares their “length”, “characters,” and the “order of characters”, whereas “objects” are compared based on “references”. The polarity of multiple numbers can also be compared using method. This write-up explained the working of the JavaScript method.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.