J
J
Jacket2020-04-14 14:45:31
JavaScript
Jacket, 2020-04-14 14:45:31

How to set default value to true/false?

How to set the default value of a variable if it is boolean?
Is it possible to do this through ||some tricky way or do I need to carry out some additional check?

function myFn(a) {
  var b = a || true;
  console.log(b);
}

myFn(false); //true

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mesuti, 2020-04-14
@Jacket_B

The "default" argument value is specified inside the function's argument list

function myFn(a = true) {
  var b = a || true;
  console.log(b);
}

myFn(false); //true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question