JavaScript String Type

Comparison

We can compare two strings and determine equality and inequality. We can also use the less-than and greater-than operators to find the longest string or in the case of equal length compare them alphabetically. All of these operators return a Boolean value.

We determine equality with the value equality operator == or the value and type equality operator ===. Inequality have their own counter parts: != and !==. We recommend always using === as the behavior is more predictable than loose equality.

Code example: equality/inequality

const quote = "The heart of wisdom is tolerance."
const sameQ = "The heart of wisdom is tolerance."
const other = "The harder the world, the fiercer the honour."
console.log(quote === sameQ) // true
console.log(quote === other) // false
console.log(quote !== other) // true

Strings are equal if they have equal lengths and every character is the same in each position. The equality check is case-sensitive.

Code example: less-than/greater-than

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