Check if Strings are Equal in JavaScript Last Updated : 11 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following ways to check for string equality in JavaScript:1. Using strict equality operator - Mostly UsedThis operator checks for both value and type equality. it can be also called as strictly equal and is recommended to use it mostly instead of double equals. JavaScript let s1 = 'abc'; let s2 = 'abc'; if (s1 === s2) { console.log('Equal'); } else { console.log('Not Equal'); } OutputEqual 2. Using double equals (==) operatorThis operator checks for value equality but not type equality. we are checking the equality of the strings by using the double equals(==) operator. JavaScript let s1 = '42'; let s2 = '42'; if (s1 == s2) { console.log('Equal'); } else { console.log('Not Equal'); } OutputEqual 3. Using String.prototype.localeCompare() methodThis method compares two strings and returns a value indicating whether one string is less than, equal to, or greater than the other in sort order. JavaScript let s1 = 'hello'; let s2 = 'geeks for geeks'; let res = s1.localeCompare(s2); if (res === 0) { console.log('Equal'); } else { console.log('Not Equal'); } OutputNot Equal 4. Using String.prototype.match() methodThis method tests a string for a match against a regular expression and returns an array of matches. JavaScript let s1 = 'hello geeks'; let s2 = 'hello geeks'; let res = s2.match(s1); if (res) { console.log('Equal'); } else { console.log('Not Equal'); } OutputEqual 5. Using String.prototype.includes() MethodThe includes() method can determine whether one string contains another string. While it is not specifically designed for strict equality checks, it can be adapted for such purposes by ensuring both strings are fully contained within each other. JavaScript let s1 = 'hello geeks'; let s2 = 'hello geeks'; if (s1.includes(s2) && s2.includes(s1)) { console.log('Equal'); } else { console.log('Not Equal'); } OutputEqual Comment More infoAdvertise with us Next Article Check if Strings are Equal in JavaScript alphanumeric91 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads Check for Substring in JavaScript Given two strings, check if one string is substring of another."bce" is substring of "abcde""ae" is not substring of "abcde"Empty String is a substring of all stringsUsing includes() - Most used and Simplest MethodThe includes() method checks whether a string contains a substring. JavaScriptlet s = 2 min read Check two given strings are isomorphic in JavaScript Two strings are said to be isomorphic if it is possible to map every character of the first string to every character of the second string. Basically, in isomorphic strings, there is a one-to-one mapping between every character of the first string to every character of the second string. We can also 5 min read JavaScript - Check if All Array Values Are Equal Here are the different methods to check if all values of an array are equal or not in JavaScript1. Using Array.every() methodFirst, get the array of elements. Pass it to an arrow function, which calls every() method on each array element and returns true if each element matches the first element of 3 min read Check if two strings are permutation of each other in JavaScript In this approach, we are going to discuss how we can check if two strings are permutations of each other or not using JavaScript language. If two strings have the same number of characters rather than having the same position or not it will be a permutation. Example: Input: "pqrs" , "rpqs"Output: Tr 4 min read How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi 3 min read Check if a variable is a string using JavaScript Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.Below 3 min read Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read JavaScript- Arrays are Equal or Not These are the following approaches to compare two arrays in JavaScript:1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the string 4 min read How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript 2 min read JavaScript - Compare the Case Insensitive Strings Here are the different methods to compare the case-insensitive strings in JavaScript.1. Using toLowerCase() or toUpperCase() MethodsThis is the most simple and used approach. Convert both strings to the same case and then compare them.JavaScriptconst comStr = (s1, s2) => s1.toLowerCase() === s2.t 2 min read Like