Practice comparing different values in JavaScript

Posted by

JavaScript has several ways to compare different values, including:

  1. Equality (==): This compares values for equality, but does not check for data type. For example, 1 == “1” would evaluate to true.
  2. Identity (===): This compares values for both equality and data type. For example, 1 === “1” would evaluate to false.
  3. Greater than (>) and less than (<): These operators compare values to see if one is greater or less than the other. For example, 5 > 3 would evaluate to true.
  4. Greater than or equal to (>=) and less than or equal to (<=): These operators compare values to see if one is greater than or equal to, or less than or equal to the other. For example, 5 >= 3 would evaluate to true.
  5. Object comparison: JavaScript compares objects by reference, not by value. So two objects with the same properties and values will not be equal to each other if they are different objects.

It’s important to note that the equality and identity operators have different semantics, and you should use the one that makes sense for your use case.

Example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const a = 5;
const b = 3;
console.log(a > b); // true
console.log(a < b); // false
console.log(a >= b); // true
console.log(a <= b); // false
console.log(a == b); // false
console.log(a === b); // false

[/dm_code_snippet]

You can also use the comparison operators with strings, which compares their lexicographical order.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

console.log("a" > "b"); // false
console.log("a" < "b"); // true
console.log("apple" > "banana"); //false

[/dm_code_snippet]

JavaScript also provides some additional comparison operators and methods:

  1. Object.is(value1, value2): This method compares values for both equality and data type, similar to the identity operator (===). However, it has some additional behavior for special cases, such as handling NaN and -0 values differently.
  2. Array.prototype.includes(value): This method checks if an array includes a certain value. It returns a boolean value indicating whether the value was found in the array.
  3. String.prototype.includes(value): This method checks if a string includes a certain value. It returns a boolean value indicating whether the value was found in the string.
  4. String.prototype.startsWith(value): This method checks if a string starts with a certain value. It returns a boolean value indicating whether the string starts with the given value.
  5. String.prototype.endsWith(value): This method checks if a string ends with a certain value. It returns a boolean value indicating whether the string ends with the given value.

Example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const a = [1, 2, 3, 4];
console.log(a.includes(2)); // true
console.log(a.includes(5)); // false

const b = "Hello World";
console.log(b.includes("llo")); // true
console.log(b.startsWith("He")); // true
console.log(b.endsWith("rld")); // true

[/dm_code_snippet]

It’s important to note that these methods and operators can be used with various data types like strings, numbers, arrays, and objects. Therefore, it’s essential to use the appropriate method or operator based on the data type you are working with to ensure correct behavior.