Logical NOT on Boolean Object always return false in Javascript -
this question has answer here:
why logical not operator in javascript returns different result between boolean value , boolean object? consider following example.
!true // false !false // true !(new boolean(true)) // false !(new boolean(false)) // false
from spec, says value being evaluated converted toboolean
. toboolean return true if argument object, , return if argument boolean.
digging further, toboolean being used in other places if statement , conditional operator, consider following example:
var = (new boolean(false)) ? "unexpected" : "expected"; console.log(a); // unexpected
the question: boolean object object, or boolean? should not evaluate boolean object boolean?
update
my question marked duplicate question this. question doesn't have satisfactory answers because none answers question, is boolean object object, or boolean? should not evaluate boolean object boolean?
simply knowing boolean object object not enough, why exists , proper way of dealing and/or designing objects boolean object still left unanswered.
an
object
, no matter if has properties or not, never defaultsfalse
...
new boolean(true)
return object
not boolean
, !{}
going false
{}
truthy
value (boolean({})
evaluated true
)
while dealing boolean
factory function, not create new instance using new
(as create new instance of boolean
, return object
)
boolean(input)
return primitive-boolean
value of specified expression
used comparison
from docs, the boolean object object wrapper boolean value
()
description: the value passed first parameter converted
boolean
value, if necessary. if value omitted or0, -0, null, false, nan, undefined, or empty string ("")
, object has initial value offalse
. other values, including object or string "false", create object initial value oftrue
.
do not confuse primitive boolean values true , false true , false values of boolean object.
any object value not undefined
or null
, including boolean object
value false, evaluates true
"when passed conditional statement."[reference]
for example, condition in following if statement evaluates true
var x = new boolean("false"); if (x) { console.log('x true'); } var y = new boolean(false); if (y) { console.log('y true'); }
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Comments
Post a Comment