Check if Strings are Equal in JavaScript Last Updated : 23 Jul, 2025 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 Create Quiz Comment A alphanumeric91 Follow 0 Improve A alphanumeric91 Follow 0 Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like