Introduction: This is the second article in the new series about the nice parts of Java Script.
Its about advantages of an un-typed language and short script code:
// 1. undefined values can be evaluated to false
var value = undefined;
if (!value) {
console.log("No value");
}
// 2. empty strings can be evaluated to false too
value = "";
if (!value) {
console.log("No or empty string");
}
// 3. similar with arrays
value = [];
if (!value || !value.length) {
console.log("No or empty array ");
}
// 4. all in combination
value = [
"", "Foo", "Bar",
];
if (!value || !value.length || !value[0]) {
console.log("No or empty array or empty first value");
}
This will give:
> No value
> No or empty string
> No or empty array
> No or empty array or empty first value
Happy Scripting
Jan