Why is this not possible?
var value = null;
if(value == (null || ""))
{
//do something
}
I would like to check a value if it is null or empty without using the variable over and over again.
Why is this not possible?
var value = null;
if(value == (null || ""))
{
//do something
}
I would like to check a value if it is null or empty without using the variable over and over again.
Use !value
. It works for undefined
, null
and even ''
value:
var value = null;
if(!value)
{
console.log('null value');
}
value = undefined;
if(!value)
{
console.log('undefined value');
}
value = '';
if(!value)
{
console.log('blank value');
}
If we split the condition into its two relevant parts, you first have null || ""
. The result of that will be equal to the empty string ""
.
Then you have value == ""
., which will be false
if value
is null
.
The correct way to write your condition is value == null || value == ""
.