what is some real use of checking identical type === | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

what is some real use of checking identical type ===

29th Oct 2016, 1:26 AM
Shekhar Bahuguna
Shekhar Bahuguna - avatar
4 Answers
+ 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.
29th Oct 2016, 10:02 AM
MarquisBs
MarquisBs - avatar
+ 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.
29th Oct 2016, 9:53 AM
MarquisBs
MarquisBs - avatar
+ 1
I got a scenario like number and a number string comparison
29th Oct 2016, 9:53 AM
Shekhar Bahuguna
Shekhar Bahuguna - avatar
- 2
плраа
30th Oct 2016, 11:55 AM
Serega555tut
Serega555tut - avatar