Javascript Check Undefined or Null or Empty

Developers often need to check variables. The value of variable can be Undefined, Null, Empty. We will show you ways to handle this situation. There are two ways to check a variable is undefined, null, empty. First, you can handle it with comman method. Second, you can handle it with efficient method.





This is common way

[js]
var second, first = "something";

if (first !== null || typeof first !== "undefined" || first !== ”) {
second = first;
}

console.log(second);
[/js]

and This is efficient way

[js]
var second, first = "something";

second = first || "";

console.log(second);
[/js]

You can test these examples. This is shorthand coding technique. If you use this technique, your codes could be more readable.