How to compare two strings in JavaScript
In this post we will give you information about How to compare two strings in JavaScript. Hear we will give you detail about How to compare two strings in JavaScriptAnd how to use it also give you demo for it if it is necessary.
We can use the comparison operator to compare two strings in JavaScript.
In this example, we are using the triple equals (===) comparison operator instead of double equals (==) to avoid the type coercion.
let str1 = "loss";let str2 = "loss";console.log(str1 === str2); // true
let a = "c124";let b = "c122";console.log(a === b); // false
The triple equals (===) operator only returns true when both values and types are same otherwise it returns false.
Let’s write our own function.
function strCompare(str1,str2){ return str1 === str2 ;}// usageconsole.log(strCompare('king','king')); // trueconsole.log(strCompare('open','close')); // false
Hope this code and post will helped you for implement How to compare two strings in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs