This is a two part question. And is different than other questions that are based on equality in JavaScript because it deals with a specific warning that is displayed in the IDE "Visual Studio Code" and why that warning message is or is not in error.
I'm getting the following warning message in Visual Studio code:
Operator '===' cannot be applied to types 'number' and => 'string'
on the following line of code:
var x = 23;
var y = "23";
x.valueOf() === y.valueOf() ?
console.log("Types ARE the same")
: console.log("Types ARE NOT the same");
Now this technique definitely works for me and I understand the reason for using a triple equal for this type of thing rather than a double equal. This same code I have learned in several fundamentals courses at codeschool and pluralsight as a way to generate a boolean value based on the type of two different values where the product or returned value is that of either true
or false
and the technique can come in handy.
MY QUESTIONS:
How is the code above any different from:
x === y ?
console.log("Types ARE the same")
: console.log("Types ARE NOT the same");
in this simple example of comparing two simple values and their types, is there something additional I can do by using the valueOf()
method along with it?
Also is Visual Studio Code wrongly generating this warning and if not is there a better way to do this type of check?
Because no matter what method I use above I get the same warning error in Visual Studio Code, but it works fine for me in practice.