In this short JS tutorial, you’ll learn how to compare strings and see code examples.
To determine whether the strings are equal, you can use the strict equality operator
===. It returns
false if the strings are different and
true, if they’re the same.
const s1 = 'learn'; const s2 = 'today'; console.log(s1 === 'learn'); // true console.log(s1 === s2); // false
Comparing the strings using strict equality
=== always analyzes the case of the letters, meaning that capital letters are different from the small ones.
If you need to find which of two strings is longer, then the operators "greater than" and "lower than" won’t suit you well. They compare the characters of a string in alphanumeric order one by one and consider the length of the strings at the very end.
In JS, every string has the
length property. By comparing the value of this property in different strings, we’ll get to know which of them is longer.
includes. Remember, the function
contains exists in Java, but it’s deprecated and replaced by