How to get value from Object, with default value

Generally, one can use the or operator to assign a default when some variable evaluates to falsy:

var foo = couldBeUndefined || "some default";

so:

var arg1 = config.args.arg1 || "test";
var arg2 = config.args.arg2 || "param2";

assuming that config.args is always defined, as your example code implies.


With ECMAScript 2020 it may be safer to use Nullish coalescing operator than or operator.

let foo = obj.maybeUndefined ?? "default value";

JS treats 0, -0, null, false, NaN, undefined, or the empty string ("") as falsy. So, using or operator for setting a default value in the following example may cause unexpected results

let obj = { maybeUndefined: 0 };
let foo = obj.maybeUndefined || 1; // foo == 1

Here, foo becomes equal to 1, when maybeUndefined is actually defined. If foo should be assigned a default value only when maybeUndefined is actually undefined or null, then we can use Nullish coalescing operator.

let obj = { maybeUndefined: 0 };
let foo = obj.maybeUndefined ?? 1; // foo == 0

let obj = {};
let foo = obj.maybeUndefined ?? 1; // foo == 1