How to use Array.from() and Array.prototype.join() Methods In Javascript
In this approach, we are using the from() and join() methods in JavaScript. Here, we are converting the input strings into an array of characters, then comparing these characters using the (===) operator, and then joining them back to strings. If the strings are the same, then the output is true; otherwise, it is false.
Syntax:
Array.from(str1).join('') === Array.from(str2).join('')
Example: This example shows the use of the above-explained approach.
const sameString = (
inputString1,
inputString2 ) => {
return (
Array.from(inputString1).join(
"") ===
Array.from(inputString2).join(
"")
);};
console.log(
sameString("geeks", "geeks")
);
Output
true
JavaScript Program to Check if Two Strings are Same or Not
In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.
Examples:
Input: str1 = Geeks, str2 = Geeks
Output: True. Strings are the Same
Input: str1 = Geeks, str2 = Geek
Output: False. Strings are not Same
Table of Content
- Using localCompare() Method
- Using startsWith() and endsWith() Methods
- Using the String.prototype.match() Method
- Using Array.from() and Array.prototype.join() Methods
- Using Iterative Character Comparison