+ 2
what is some real use of checking identical type ===
4 odpowiedzi
+ 3
Normaly when you check variables you expect them to be of some type.
An Example: Your code manages users and you need a value to be a number (probably Integer) for example the age. You dont want it to suddenly be a string. And even though it often wouldnt matter it would show that there is an inconsistency in your code and that is never a good thing.
You definetly want to check the type. :)
I found you more reference here that explains it in the very detail: http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons#359509
Have a look at it. Its very well formulated.
Here is an excerpt:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
It shows that == not only doesnt show inconsistency in your own code but also works inconsistent itself and is hard to remember when it does what.
+ 2
You actually should only use ===.
The difference between '==' an '===' is its strictness.
//Example Variables to show case
var nr = 23;
var nrInAString = '23';
'==' for Example will give you true when:
if (nr == nrInAString) document.log('They are equal');
// output is 'They are equal'
'===' will check the type of the variables and will give you false:
if (nr === nrInAString) document.log('They are equal');
// wont output 'They are equal'
Hope that helped.
+ 1
I got a scenario like number and a number string comparison
- 2
плраа