Mots clés : javascriptnullcomparisonundefinedjavascript
96
if( value ) { }
if( typeof foo !== 'undefined' ) { // foo could get resolved and it's defined }
90
return value === undefined || value === null;
return value == null; // also returns true if value is undefined
72
function isEmpty(value){ return (value == null || value.length === 0); }
undefined // Because undefined == null null [] ""
function isEmpty(value){ return (value == null || value === ''); }
69
if (typeof value !== 'undefined' && value) { //deal with value' };
51
function typeOf(obj) { return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase(); }
function typeOf(obj) { const { toString } = Object.prototype; const stringified = obj::toString(); const type = stringified.split(' ')[1].slice(0, -1); return type.toLowerCase(); }
typeOf(); //undefined typeOf(null); //null typeOf(NaN); //number typeOf(5); //number typeOf({}); //object typeOf([]); //array typeOf(''); //string typeOf(function () {}); //function typeOf(/a/) //regexp typeOf(new Date()) //date typeOf(new WeakMap()) //weakmap typeOf(new Map()) //map