How do I determine if variable is undefined
or null
?
My code is as follows:
var EmpN
Best way:
if(typeof variable==='undefined' || variable===null) {
/* do your stuff */
}
I know I'm 10 years late. But I'll leave my answer here just in case anybody needs a short method.
Installing Lodash in your project could be useful because of the helper functions that could come in handy in these situations.
Using ES6 modules, import would look like this:
import isNull from 'lodash/isNull';
import isUndefined from 'lodash/isUndefined';
import isNil from 'lodash/isNil';
Would be better if only the used functions are imported.
Lodash's isNull checks if value is null.
const value = null;
if(isNull(value)) {
// do something if null
}
lodash's isUndefined checks if value is undefined.
const value = undefined;
if(isUndefined(value)) {
// do something if undefined.
}
isNil Checks if value is null OR undefined. I prefer this method over other two, because it checks both undefined and null.
Let's look at this,
let apple; // Only declare the variable as apple
alert(apple); // undefined
In the above, the variable is only declared as apple
. In this case, if we call method alert
it will display undefined.
let apple = null; /* Declare the variable as apple and initialized but the value is null */
alert(apple); // null
In the second one it displays null, because variable of apple
value is null.
So you can check whether a value is undefined or null.
if(apple !== undefined || apple !== null) {
// Can use variable without any error
}
Probably the shortest way to do this is:
if(EmpName == null) { /* DO SOMETHING */ };
Here is proof:
function check(EmpName) {
if(EmpName == null) { return true; };
return false;
}
var log = (t,a) => console.log(`${t} -> ${check(a)}`);
log('null', null);
log('undefined', undefined);
log('NaN', NaN);
log('""', "");
log('{}', {});
log('[]', []);
log('[1]', [1]);
log('[0]', [0]);
log('[[]]', [[]]);
log('true', true);
log('false', false);
log('"true"', "true");
log('"false"', "false");
log('Infinity', Infinity);
log('-Infinity', -Infinity);
log('1', 1);
log('0', 0);
log('-1', -1);
log('"1"', "1");
log('"0"', "0");
log('"-1"', "-1");
// "void 0" case
console.log('---\n"true" is:', true);
console.log('"void 0" is:', void 0);
log(void 0,void 0); // "void 0" is "undefined"
And here are more details about ==
(source here)
BONUS: reason why ===
is more clear than ==
(look on agc answer)
If the variable you want to check is a global, do
if (window.yourVarName) {
// Your code here
}
This way to check will not throw an error even if the yourVarName
variable doesn't exist.
if (window.history) {
history.back();
}
window
is an object which holds all global variables as its properties, and in JavaScript it is legal to try to access a non-existing object property. If history
doesn't exist then window.history
returns undefined
. undefined
is falsey, so code in an if(undefined){}
block won't run.
The easiest way to check is:
if(!variable) {
// If the variable is null or undefined then execution of code will enter here.
}