4 Answers
New AnswerI tend to use the opposite to the others here to prevent accidental assignment of null. eg if(null == variable)
var x; if(x == null){ console.log("the value is null"); } else { console.log("the value is something else"); }
if (x != null) continue; log.("value isn't null") else { log.("value is null or other value"} }; You can easily invert it, if (x == null) log("value is null"); else log("value isn't null"); I just saw the tag was #ecma sorry but the logic is almost the same. You can do it like this; if( typeof somevar !== 'undefined' ) { /* ecma evaluators include * null *undefined *NaN *empty string ("") *0 *false */ }
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message