Home > Blockchain >  Is there a better way to handle default value?
Is there a better way to handle default value?

Time:04-24

There are too many times I have write something reassembling this:

const foo = some_expression ? some_expression : some_default_value;

This is really long already and normally an expression would be even longer. Is there a concise way to do this in JS/TS?

I know I can define a function but this is still not ideal in my opinion.

CodePudding user response:

const foo = truthy || whatever;

If they are parameters passed into your function you can set a default when passed:

function foo(bar = 'biz') {

}

CodePudding user response:

const foo = some_expression || some_default_value

for more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

  • Related