JavaScript comparison operators are used to compare two values and return a boolean value (true or false) depending on whether the comparison is true or false.
Comparison operators are an integral part of JavaScript and help you write useful conditions for if statements.
One of the key JavaScript concepts to understand is the difference between “loose” and “strict” comparisons:
- JavaScript performs type juggling with “loose” comparisons. For example, when using free comparison, the number 1 matches the string “1”.
- When JavaScript performs “strict” comparisons, variables only match if their data types match.
JavaScript Comparison operators in JavaScript
= (equal)
he equality operator returns the boolean value true if both operands are equal (type and value). Arrays and objects are tested for deep equality. Arrays must have the same values in the same order. Objects must have identical key/value pairs (order does not matter)otherwise return false.
Example:
1+1 = 2 => True
"Hello" = "World" => False
!= (not equal)
The inequality operator returns the boolean value false if both operands are equal (type and value, depth Equality) returns true otherwise. JavaScript uses this operator to check if two values are equal.This comparison is done loosely, so the data types can be different.
Example:
1+1 != 3 => True
"Hello" != "World" => True
The greater than operator returns a boolean true if the left side is numerically greater than the right side otherwise return false. > operator Returns true or false depending on whether the first value is greater than the second value.
Example:
22 / 7 > 3 => true
5 > 5 => false
The less than operator returns a Boolean true if the left side is numerically less than the right side. otherwise false is returned. < operator returns true or false depending on whether the first value is less than the second value.
Example:
22 / 7 < 3 => false
5 < 5 => false
- >= (greater than or equal to)
The greater than or equal to operator returns the boolean value true if LHS is numerically greater than or equal to RHS otherwise returns false. >= operator returns true or false depending on whether the first value is greater than or equal to the second value.
Example:
22 / 7 >= 3 => true
5 >= 5 => true
- <= (less than or equal to)
The less than or equal operator returns the boolean value true if LHS is numerically less than or equal to the same as RHS otherwise returns false. <= operator returns true or false depending on whether the first value is less than or equal to the second value.
Example:
22 / 7 <= 3 => false
5 <= 5 => true
The array (sequence) inclusion operator returns the boolean value true if the LHS value is contained in an array of values at RHS otherwise return false. If RHS is a single value, it is treated as a single element array.
Example:
"world" in ["hello", "world"] => true
"hello" in "hello" => true
library.books["Aho" in authors].title