colorX is my parameter from some function.
colorX = typeof colorX !== ('undefined' || 'null' || '') ? colorX : 'abc';
Here for 'undefined' I'm getting abc as value. But when I pass null or 'null' or empty string as parameter input. I'm getting error.
Also what is the difference between null and 'null'?
CodePudding user response:
Explanation
null
vs 'null'
When using typeof
, it expects a string version of the type, such as 'undefined'
, 'function'
, 'bigint'
, and so on. However, specifically for the null
type it functions as an object:
console.log(typeof null); // expected output: object
console.log(typeof 'strExample'); // expected output: string
// We can return a boolean by doing so:
let variable = 10;
console.log(typeof variable === 'string'); // expected output: false
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
The explanation as to why can be found on MDN:
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was
0
.null
was represented as the NULL pointer (0x00
in most platforms). Consequently,null
had 0 as type tag, hence thetypeof
return value"object"
. (reference)A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === 'null'
.
In any other scenario, 'null'
, 'undefined'
, and 'string'
would be identified as strings since they are surrounded in ''
's, where this is what @reyno was referring to.
See all accepting values for typeof
here.
Checking for null
// null will be identified as an "object" type; checking
// null with typeof is redundant as {} can be the same as null.
console.log(typeof null); // expected output: object
console.log(typeof {}); // expected output: object
// so we can just do:
let colorX = null;
console.log(colorX === null); // expected output: true
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Resulting Code
Since falsy values can be any of these:
Besides
false
, possible falsy expressions are:null
,NaN
,0
, the empty string (""
), andundefined
.
We can simply do:
colorX = colorX || 'abc';
As @ivar claims.
CodePudding user response:
Using the ternary operator you could simplyfy this to
colorX = colorX ? colorX : 'abc';
The Conditional (ternary) operator syntax is
condition ? exprIfTrue : exprIfFalse
and according to the MDN docs
...null, NaN, 0, the empty string (""), and undefined. If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.
But alternatively and even shorter (in case you are checking for all 'falsy' values) you might as well use the logical OR
colorX = colorX || 'abc';
Since you say you want to check for all falsy values except for 0 you can combine both
let v = 0
v = v || v === 0 ? v : 'value'
console.log(v) // 0
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Notice that
null, undefined, 0
with quotes simply become strings containing the characters without "functional meaning"