This is an extremely basic question, I know, but I couldn't understand what's going on from Google and Stack Overflow.
I looked here and here to learn how to pare strings in JavaScript. Neither mentioned triple equals (===
) in their answers, and said that it's better to use your own function (str1 < str2 ? -1 : str1 > str2
).
However, going through explanations about ===
in Stack Overflow (here and here), the answers contain string parisons.
From what I saw in those answers, ===
does work for string parisons, so why wasn't it included in the string parison answers?
I'm just trying to expand my knowledge in JavaScript.
Thanks for any insight!
This is an extremely basic question, I know, but I couldn't understand what's going on from Google and Stack Overflow.
I looked here and here to learn how to pare strings in JavaScript. Neither mentioned triple equals (===
) in their answers, and said that it's better to use your own function (str1 < str2 ? -1 : str1 > str2
).
However, going through explanations about ===
in Stack Overflow (here and here), the answers contain string parisons.
From what I saw in those answers, ===
does work for string parisons, so why wasn't it included in the string parison answers?
I'm just trying to expand my knowledge in JavaScript.
Thanks for any insight!
Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Nov 22, 2015 at 18:11 NetaNeta 9016 gold badges14 silver badges31 bronze badges 4-
11
Yes,
===
can pare two strings, givingtrue
orfalse
as a result. The other examples you saw were talking about paring strings when sorting lists of them. – Pointy Commented Nov 22, 2015 at 18:12 - You're correct. Thanks! – Neta Commented Nov 22, 2015 at 18:16
-
1
i dont think theres any advantage in using
===
instead of==
when paring thing you KNOW are going to be strings.===
is usually for paring things that will be multiple types. IE.'0' !== 0
. I assume==
is faster as well, but I'm much to lazy to find you benchmarks to back that up. – Rooster Commented Nov 22, 2015 at 18:19 - 2 You should only use === when the data type of the value matters, meaning when it could result in an undesirable behavior. Like if something requires a 0 to calculate but instead gets a false or a string '0' which can result in an inaccurate calculation or a string value that could choke a process. Triple-equals are so abused nowadays in JavaScript, I've had to fix multiple bugs in mitted code to change the === to == when it wasn't necessary and caused problems (meaning it didn't matter what the data type was but checking the data type of '0' vs 0 caused the process to choke). – Davicus Commented Apr 4, 2018 at 0:23
2 Answers
Reset to default 7var str1 = "1";
var str2 = 1;
if (str1 == str2) {
//Below code executes as it's true.
console.log("Yes, value of both are equal.");
}
if (str1 === str2) {
//Below code never executes as it's false.
console.log("No, both are not equal as type differs.");
}
==
pares value but ===
pares value as well as type.
===
can be used as string parison but if you are sure that you are just paring string then ==
should be sufficient. ===
is just a better choice.
you can use both methods to pare strings, it just that you use ===
when you want to pare value AND type. what else is your query?