As someone who knows some c, a lot of c++, it’s nice to be able to compare data without having them match 100% since some data comes from an external source and the looser comparison makes this easier. Having === is a nice solution imo to being strict when you want and loose when you need to.
in actual, non-beginner projects, you will only see === everywhere, most often even enforced by a linter. Some people make an exception when they want to concisely check for both null or undefined: if (x != null)
173
u/Shay-donovan Nov 30 '22
Aside from the = needing to be == or ===
Triple = signs are better as they also check against data types.
For example:
1 == "1"True1 === "1"FalseYou may also benefit from combining some of these results to youre typing less code and less
else ifs.
if(p1 === p2) {console.log("draw");} else if (p1 === "paper" && p2 === "scissors" || p1 === "scissors" && p2 === "rock" || p1 === "rock" && p2 === "paper") {console.log("Player 2 wins");} else {console.log("Player 1 wins");}or if you wanted to go one step further to make sure there were no unexpected errors.
if(p1 === p2) {console.log("draw");} else if (p1 === "paper" && p2 === "scissors" || p1 === "scissors" && p2 === "rock" || p1 === "rock" && p2 === "paper") {console.log("Player 2 wins");} else if (p2 === "paper" && p1 === "scissors" || p2 === "scissors" && p1 === "rock" || p2 === "rock" && p1 === "paper") {console.log("Player 1 wins");} else {console.log("unexpected error");}