Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: check if variable is false or if it doesn't exist at all [duplicate]

Tags:

javascript

I want to write a function that uses argument value. If argument does not exist it will use value true by default.

var check = function(truthValue){
    var val = truthValue || true;
    console.log(val);
};

The problem is that if I pass value false to it,as it will still use default value. So how do I check if varible exists and use that value in Javascript?

like image 200
sublime Avatar asked Jan 17 '26 18:01

sublime


1 Answers

Use

if (typeof val === 'undefined') val = true;
like image 91
Denys Séguret Avatar answered Jan 20 '26 10:01

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!