Tuesday 16 January 2018

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





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?


style="font-weight: bold;">

Answer




Use




if (typeof val ===
'undefined') val = true;


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...