Friday

How to check for the existence of undeclared variable in js

You can check for the existence of undeclared variable in javascript without getting ReferenceError:
if (x !== undefined){} // gets error if x undeclared
if (x !== 'undefined'){} // gets error if x undeclared
if (typeof x !== undefined){} // gets error because typeof x returns "undefined"

if (typeof x !== 'undefined'){} //right one

No comments:

Post a Comment