You can achieve this by combining && and ||:
finalData.transactions = (data && data.b && data.b.transactions) || [];
Answer from Nico on Stack OverflowIs there a builtin function that handles checking for undefined as well as all the arguments?
No, but you can write one yourself.
So maybe I can do this?
_call(next, argument1, argument2, ... )
Yes:
function _call(fn, ...args) {
if (typeof fn == "function") return fn(...args);
}
(using ES6 rest & spread syntax, in ES5 it would be fn.apply(null, Array.prototype.slice.call(arguments, 1))
This is a bit of a hack but you might be able to use default arguments
(function(next=()=>{}){
//do stuff
next(a, b, c, d, e);
})();
So if it is not called with an argument, next will be an empty function that doesn't do anything
You can use the qualities of the abstract equality operator to do this:
if (variable == null){
// your code here.
}
Because null == undefined is true, the above code will catch both null and undefined.
The standard way to catch null and undefined simultaneously is this:
if (variable == null) {
// do something
}
--which is 100% equivalent to the more explicit but less concise:
if (variable === undefined || variable === null) {
// do something
}
When writing professional JS, it's taken for granted that type equality and the behavior of == vs === is understood. Therefore we use == and only compare to null.
Edit again
The comments suggesting the use of typeof are simply wrong. Yes, my solution above will cause a ReferenceError if the variable doesn't exist. This is a good thing. This ReferenceError is desirable: it will help you find your mistakes and fix them before you ship your code, just like compiler errors would in other languages. Use try/catch if you are working with input you don't have control over.
You should not have any references to undeclared variables in your code.
I'm afraid there's no real shorter way of doing this (well technically a minor one and then somewhat more obtuse one)
I tried playing around with the nullish operator
??, but I have to say the working solution seems uglier than what I'm a bout to propose.Essentially it boils down to the fact, you're looking for the negation of the nullish operator (or alternatively a nullish ternary), neither of which sadly exist (yet).
Here's a relatively compact solution, which just improves upon your solution and simplifies it with the usage of the && logical AND short-circuit operator
const notNullish = (value) =>
value !== null && typeof value !== 'undefined'
const foo = (a, b) => ({
...(notNullish(a) && {a}),
...(notNullish(b) && {b}),
})
console.log('foo(6, 7) =>', foo(6, 2))
console.log('foo(6) =>', foo(6))
console.log('foo() =>', foo())
console.log('foo(0, false) =>', foo(0, false))
Now as I said, there's no optimal way of doing this, because while the you can use the logical ?? to check, it cannot directly assign to object property at the same time. Best I could come up with is:
const notNullish = (key, value) => {
const nullishCheck = value ?? 'IS_NULLISH'
return nullishCheck === 'IS_NULLISH'
? {}
: {[key]: value}
}
const foo = (a, b) => ({
...notNullish('a', a),
...notNullish('b', b)
})
console.log(foo(6, 4))
console.log(foo(6))
console.log(foo())
console.log(foo(0, false))
But personally I find the second snippet kinda ugly and would probably just go with the first option, especially for the following three reasons
- In my opinion it just looks plain uglier
- The
??operator is not yet fully supported on Explorer and Node.js - Chances are not a lot of people are even familiar with the operator and it forces you to not even use it directly, but rather as an assignment check.
What about
return Object.assign({}, a, b);
It will simply ignore nullish values or spread if objects
It's been years it doesn't make sense anymore to use this construct (unless you don't know whether the variable, not the value, is undefined). undefined is now read only.
Simply use
if (entry[i].getElementsByTagName("url")[0].childNodes[0] === undefined) {
In almost all cases, typeof x === "undefined" is a bad practice.
In the specific case of a DOM element, you can also simply use
if (!entry[i].getElementsByTagName("url")[0].childNodes[0]) {
because you can't have a falsy node, and of course, when the goal is to apply a default value, just use
var foo = entry[i].getElementsByTagName("url")[0].childNodes[0] || 'baar';
(be careful that this test only works when all the parts before the the last [0] are present, it's usually convenient to use querySelector or a DOM selection API like jQuery to make everything less verbose).
var foo = entry[i].getElementsByTagName("url")[0].childNodes[0] || 'baar'