TypeScript gives you type notation, so you know what types your variables are. Thus === should be redundant, because you shouldn't need to type check.
With JavaScript types are not as obvious. Comparing with == is error prone because it will try to coerce your variables to be the same type, whereas with === it won't. Sometimes in JS types matter, so using === is preferable so you don't end up assigning a string to a number or vice versa.
In javascript I want to compare 4 == "4" and get true.
In typescript you should get and error before compile if you try to compare 4 == "4" I guess...
When I saw the === the first time, I thought it was for "deep" comparison of objects :-)
ie: {a:1, b:2} === {b:2, a:1} should be true.
So I guess we still have no answer why to use === rather than == in everyday JS code.
I've never had a reason to say "types does matter" - mysql and json is happy saving a string to a number and vice versa. Displaying entry fields or text in html as well.
I guess in a accounting environment maybe? Negatives, decimals, power of 10, etc.. You would want to make 100% sure you have a number, and if user type 10e2.123 you would need to convert to 1000.12 before saving?
I see I use parseInt if I want to add numbers.
ie:
return this.meritList.reduce( (accum,item) => accum + parseInt(item.point),0)
The question was why to ever use == rather than the ===.
You're saying you never use === in JavaScript? Do you not fall foul of the problem where 3+3 is not the same as '3'+3? Or that '0' == 0 but 0 == false and '0' == true?
I'm not sure this reasoning is sound.
TypeScript gives you type notation, so you know what types your variables are. Thus
===should be redundant, because you shouldn't need to type check.With JavaScript types are not as obvious. Comparing with
==is error prone because it will try to coerce your variables to be the same type, whereas with===it won't. Sometimes in JS types matter, so using===is preferable so you don't end up assigning a string to a number or vice versa.In javascript I want to compare 4 == "4" and get true.
In typescript you should get and error before compile if you try to compare 4 == "4" I guess...
When I saw the === the first time, I thought it was for "deep" comparison of objects :-)
ie: {a:1, b:2} === {b:2, a:1} should be true.
So I guess we still have no answer why to use === rather than == in everyday JS code.
I've never had a reason to say "types does matter" - mysql and json is happy saving a string to a number and vice versa. Displaying entry fields or text in html as well.
I guess in a accounting environment maybe? Negatives, decimals, power of 10, etc.. You would want to make 100% sure you have a number, and if user type 10e2.123 you would need to convert to 1000.12 before saving?
I see I use parseInt if I want to add numbers.
ie:
return this.meritList.reduce( (accum,item) => accum + parseInt(item.point),0)
And that's about it.
The question was why to ever use
==rather than the===.You're saying you never use
===in JavaScript? Do you not fall foul of the problem where3+3is not the same as'3'+3? Or that'0' == 0but0 == falseand'0' == true?I did a quick grep - and I see there is some place where I use ===
if (!response.constructor === Array)
if (this.photoPath === undefined)
But I use == much more frequently
Never realised that 0 == false and '0' == true - that is a nasty one.
But I've never when debugging, had an error related to == vs ===
Lucky or dumb, or dumb luck I guess?