JavaScript Comparisions

CodingTute

JavaScript

In JavaScript, you can use comparison operators to compare values and determine whether a condition is true or false. These operators include:

== (equal to): This operator checks if the value on the left is equal to the value on the right. It performs type coercion, meaning it converts the operands to the same type before making the comparison. For example:

console.log(1 == '1');  // true

=== (strict equal to): This operator is similar to the == operator, but it does not perform type coercion. It checks if the value and type on the left are equal to the value and type on the right. For example:

console.log(1 === '1');  // false

!= (not equal to): This operator checks if the value on the left is not equal to the value on the right. It also performs type coercion. For example:

console.log(1 != '1');  // false

!== (strict not equal to): This operator is similar to the != operator, but it does not perform type coercion. It checks if the value or type on the left are not equal to the value or type on the right. For example:

console.log(1 !== '1');  // true

> (greater than): This operator checks if the value on the left is greater than the value on the right. For example:

console.log(1 > 2);  // false

< (less than): This operator checks if the value on the left is less than the value on the right. For example:

console.log(1 < 2);  // true

>= (greater than or equal to): This operator checks if the value on the left is greater than or equal to the value on the right. For example:

console.log(1 >= 2);  // false
console.log(2 >= 2);  // true

<= (less than or equal to): This operator checks if the value on the left is less than or equal to the value on the right. For example:

console.log(1 <= 2);  // true
console.log(2 <= 2);  // true

You can use these operators in combination with if statements and other control structures to perform different actions based on the result of the comparison.

JavaScript String Comparisions

You can use the comparison operators discussed above to compare string values. When comparing strings, the comparison is based on the Unicode values of the characters in the strings.

For example:

console.log('a' < 'b');  // true
console.log('a' > 'b');  // false
console.log('a' == 'a');  // true
console.log('a' == 'A');  // false
console.log('a' === 'a');  // true
console.log('a' === 'A');  // false

As you can see, the comparison operators work in a similar way when comparing strings as they do when comparing other values. The == and != operators perform type coercion, while the === and !== operators do not.

It’s important to note that the comparison of strings is case-sensitive. In the examples above, the string ‘a’ is not equal to the string ‘A’ when using either the == or === operator.

When comparing multi-character strings, the comparison is based on the Unicode values of the characters in the strings.

For example:

console.log('abc' < 'def');  // true
console.log('abc' > 'def');  // false
console.log('abc' == 'abc');  // true
console.log('abc' == 'ABC');  // false
console.log('abc' === 'abc');  // true
console.log('abc' === 'ABC');  // false

As you can see, the comparison operators work in a similar way when comparing multi-character strings as they do when comparing other values. The == and != operators perform type coercion, while the === and !== operators do not.

The comparison of strings is case-sensitive. In the examples above, the string ‘abc’ is not equal to the string ‘ABC’ when using either the == or === operator.

In addition to the basic comparison operators, you can also use the String.prototype.localeCompare() method to compare strings in a more flexible way. This method allows you to specify a variety of options, such as whether the comparison should be case-sensitive or whether it should consider accent marks.

How String Comparision works in JavaScript?

When you compare two strings using a comparison operator, the JavaScript engine compares the Unicode values of the characters in the strings one by one, starting with the first character. If the values of the characters are equal, the engine moves on to the next character and continues the comparison until it finds a character whose values are not equal, or until it reaches the end of one of the strings.

For example:

console.log('abc' < 'def');  // true

In this example, the JavaScript engine starts by comparing the first characters of the two strings, ‘a’ and ‘d’. Since the Unicode value of ‘a’ is less than the Unicode value of ‘d’, the comparison returns true.

console.log('abc' > 'def');  // false

In this example, the JavaScript engine again starts by comparing the first characters of the two strings, ‘a’ and ‘d’. Since the Unicode value of ‘a’ is less than the Unicode value of ‘d’, the comparison returns false.

console.log('abc' == 'abc');  // true

In this example, the JavaScript engine compares the Unicode values of the characters in the two strings, ‘abc’ and ‘abc’, and finds that they are equal. The comparison therefore returns true.

You can find the complete JavaScript Tutorials here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.