There are three ways to check for non-null. My recommendation is to use Strict Not Version.
1. Strict not version
if (val !== null) { ... }
The Strict Not Version uses the "Strict Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6 . !== has better performance than the != operator, because the strict equality comparison algorithm has no type values.
2. Lax is not a version
if (val != 'null') { ... }
The non-strict version uses the "Abstract Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 . != has a lower performance than the !== operator, because the abstract equality comparison algorithm compares the values.
3. Double Not Version
if (!!val) { ... }
Double not version !! has better performance than both the strict non-version !== and the non-strict version != ( https://jsperf.com/tfm-not-null/6 ). However, it will determine "Falsey" values, such as undefined and NaN , in False ( http://www.ecma-international.org/ecma-262/5.1/#sec-9.2 ), which may lead to unexpected results, and it has better readability since null is not explicitly specified.
tfmontague Jan 15 '16 at 18:53 2016-01-15 18:53
source share