javascript - What is the benefit of using === operator to test for undefined instead of using == operator? -
i test whether javascript variable defined or not, using following test:
if (typeof == 'undefined') = 'default value' however, have seen many people suggesting safer use following instead:
if (typeof === 'undefined') = 'default value' i aware of difference between == , === operators unable understand why typeof == 'undefined' unsafe.
could please scenario in first code example unsafe or lead issues? also, advantages of using latter syntax on first?
i unable understand why
typeof == 'undefined'unsafe
it's not unsafe, it's fine. typeof operator guaranteed return string, you'll comparing 2 strings. abstract equality algorithm behave strict equality.
Comments
Post a Comment