What is the correct way to check for string equality in JavaScript?

Asked
Active3 hr before
Viewed126 times

8 Answers

checkcorrectstring
90%

So the best way to check for equality is using the === operator because it checks value as well as type of both operands.,If you want to check for equality between two objects then using String.prototype.valueOf is the correct way.,What is the correct way to check for equality between Strings in JavaScript?,If you know they are strings, then there's no need to check for type.

If you know they are strings, then there's no need to check for type.

"a" == "b"

However, note that string objects will not be equal.

new String("a") == new String("a")

Call the valueOf() method to convert it to a primitive for String objects,

new String("a").valueOf() == new String("a").valueOf()
load more v
88%

Generally, if the strings contain only ASCII characters, you use the === operator to check if they are equal.,Since s1 and s2 have the same characters, they are equal when you compare them using the === operator:,When the strings contain characters that include combining characters, you normalize them first before comparing them for equality.,A combining character is a character that applies to the precedent base character to create a grapheme.

Suppose you have the following two strings:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
const s1 = 'Hi';
const s2 = 'Hi';
Code language: JavaScript(javascript)
load more v
72%

When comparing a number to a string, try to convert the string to a numeric value. , If one of the operands is Boolean, convert the Boolean operand to 1 if it is true and +0 if it is false. , If one of the operands is an object and the other is a number or a string, try to convert the object to a primitive using the object's valueOf() and toString() methods. , If one operand is null and the other is undefined, return true.

x == y
load more v
65%

Let’s detail into how to correctly compare strings in JavaScript. Before starting, I’m going to familiarize you with the terms of grapheme (a unit of writing) and combining character (specialized character that modify the look of a base character).,You’re safe to compare strings directly when their characters are from the Basic Multilingual Plane.,Here’s a formal definition of a grapheme:,Having a better understanding of graphemes and combining characters, here are a couple of rules for safer strings comparison in JavaScript.

Let’s compare 2 strings str1 and str2:

javascriptconst str1 = 'Hello!';
const str2 = 'Hello!';
str1 === str2; // => true
load more v
75%

new String("a").valueOf() == new String("a").valueOf()
load more v
40%

The equals() method compares two strings, and returns true if the strings are equal, and false if not.,Tip: Use the compareTo() method to compare two strings lexicographically.,Compare strings to find out if they are equal:,false - if the strings are not equal

Compare strings to find out if they are equal:

String myStr1 = "Hello";
String myStr2 = "Hello";
String myStr3 = "Another String";
System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal
System.out.println(myStr1.equals(myStr3)); // false
load more v
22%

Compare the first character of both strings.,If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.,Otherwise, if both strings’ first characters are the same, compare the second characters the same way.,o is greater than e. Stop here. The first string is greater.

alert(2 > 1); // true (correct)
alert(2 == 1); // false (wrong)
alert(2 != 1); // true (correct)
load more v
60%

What is the correct way to check for equality between Strings in JavaScript?,If you want to check for equality between two objects then using String.prototype.valueOf is the correct way.,So the best way to check for equality is using the === operator because it checks value as well as type of both operands.,There are actually two ways in which strings can be made in javascript.

If you know they are strings, then there's no need to check for type.

"a" == "b"

However, note that string objects will not be equal.

new String("a") == new String("a")

Call the valueOf() method to convert it to a primitive for String objects,

new String("a").valueOf() == new String("a").valueOf()
load more v

Other "check-correct" queries related to "What is the correct way to check for string equality in JavaScript?"