I recently had to deal with some javascript and came across this code:
name: function(arg){
//...
var arg = arg || new Object();
//...
}
TBH, I'm not quite fit in js, but as far as I know, this means that if arg
is not defined, arg
will be new Object();
My question now is, how does js know that this isn't considered a boolean interpretation? Or how excatly does this comparison work?
Like, javascript does have dynamic data types, so I would have assumed if arg
was a boolean, (and if I'm not wrong, the boolean value of new Object();
is always true) arg
would be true after running the function, no matter the previous value of arg
was.
However, I tested that, using various parameters (and again, I'm new to js) and the results are quite strange to me:
Using:
function name(arg) {
var arg = arg || new Object();
return arg;
}
Results:
name() = [object Object]
name(1) = 1
name(true) = true
name(false) = [object Object]
name('true') = true
name('false') = false
name(null) = [object Object]
I would have expected these results, except for name(false) = [object Object]
. What's the reason for that result? I couldn't really find a satisfying answer.
Regards, Simon