You can achieve this by combining && and ||:

finalData.transactions = (data && data.b && data.b.transactions) || [];
Answer from Nico on Stack Overflow
🌐
DEV Community
dev.to › digitalpollution › mastering-javascript-shorthands-33mg
Mastering JavaScript Shorthands - DEV Community
August 9, 2023 - Version 3 may cause trouble, as ... 0 · Default function parameters are used, if the argument is undefined, while the || checks for false....
🌐
GitHub
gist.github.com › pongstr › 9f273cdfbd5b600bc758
Shorthand Coding Techniques. Original post by @samdeering http://www.sitepoint.com/shorthand-javascript-techniques/ · GitHub
// Longhand if (variable1 !== null || variable1 !== undefined || variable1 !== '') { var variable2 = variable1; } // Shorthand var variable2 = variable1 || ''; // Browser Test: //null value example var variable1 = null, variable2 = variable1 ...
🌐
LinkedIn
linkedin.com › pulse › 25-javascript-shorthand-techniques-you-need-everyday-hatem-ahmad
25 JavaScript shorthand techniques you need everyday
September 24, 2021 - By default, JavaScript will set function parameters to undefined if they are not passed a value. Some other languages will throw a warning or error. To enforce parameter assignment, you can use an if statement to throw an error if undefined, or you can take advantage of the ‘Mandatory parameter shorthand’.
🌐
Stack Abuse
stackabuse.com › javascript-check-if-variable-is-a-undefined-or-null
JavaScript: Check if Variable is undefined or null
March 29, 2023 - It can be used as the shorthand version for checking both: let a = null; console.log(_.isNull(a)); // true console.log(_.isUndefined(a)); // false console.log(_.isNil(a)); // true · In this short guide, we've taken a look at how to check if a variable is null, undefined or nil in JavaScript, using the ==, === and typeof operators, noting the pros and cons of each approach.
🌐
SitePoint
sitepoint.com › blog › javascript › 25+ javascript shorthand coding techniques
25+ JavaScript Shorthand Coding Techniques — SitePoint
November 15, 2024 - Some of the most common shorthand techniques include the Ternary Operator, which is a shorter way of writing an if-else statement, and the Nullish Coalescing Operator, which returns the first argument if it’s not null or undefined.
🌐
Qubits & Bytes
qubitsandbytes.co.uk › javascript › shorthand-null-handling-in-javascript
Shorthand Null Handling in Javascript – Qubits & Bytes
February 15, 2025 - The nullish coalescing operator (??) is a conditional operator, which returns a second value should the first value be either null or undefined. console.log('JS is cool' ?? 'hello world'); // 'JS is cool' console.log(null ??
Find elsewhere
🌐
Medium
medium.com › codeart-mk › 10-js-shorthand-techniques-2763f0ef44b2
10 JS Shorthand Techniques. If you are reading this, it means you… | by Antonija Dimoska | Medium
February 22, 2024 - This works only if the key name is the same as the variable name assigned to it. There is another useful js shorthand.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
Note: The strict equality operator (as opposed to the standard equality operator) must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. This is because null is not equivalent to undefined. See Equality comparison and sameness for details.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
Note: The undefined is not a reserved ... test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined')....
Top answer
1 of 3
2

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

  1. In my opinion it just looks plain uglier
  2. The ?? operator is not yet fully supported on Explorer and Node.js
  3. 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.
2 of 3
1

What about

return Object.assign({}, a, b);

It will simply ignore nullish values or spread if objects

🌐
Medium
tapajyoti-bose.medium.com › 7-shorthand-optimization-tricks-every-javascript-developer-should-know-bf4e136d4497
7 Shorthand Optimization Tricks every JavaScript Developer Should Know 😎 | by Tapajyoti Bose | Medium
March 1, 2025 - This article will cover a plethora of JavaScript Shorthand Optimization tricks that can help you write better code, and also make sure this is NOT your reaction when you encounter them: Often you might need to check if a string is equal to one of the multiple values, and can become tiring extremely quickly.
🌐
LogRocket
blog.logrocket.com › home › 18 javascript and typescript shorthands to know
18 JavaScript and TypeScript shorthands to know - LogRocket Blog
June 4, 2024 - See an example of the object property assignment shorthand below: ... Dot notation allows us to access the keys or values of an object. With optional chaining, we can go a step further and read keys or values even when we are not sure whether they exist or are set. When the key does not exist, the value from optional chaining is undefined. This helps us avoid unneeded if/else check conditions when reading values from objects and unnecessary try/catch to handle errors thrown from trying to access object keys that don’t exist.
🌐
HackerNoon
hackernoon.com › 12-amazing-javascript-shorthand-techniques-fef16cdbc7fe
12 Good JavaScript Shorthand Techniques | HackerNoon
June 18, 2017 - Update 1: Due to a lot of polarizing comments (like loved or hated the article) I just want to be clear that shorthand are useful sometimes and sometimes not, depends on which context, don’t take shorthand code as the best of any situation!