Should you use == or === to compare with Boolean / boolean values in js? Problem is:
> true == new Boolean(true)
< true
> true === new Boolean(true)
< false
same goes for String:
> 'foo' == new String('foo')
< true
> 'foo' === new String('foo')
< false
I understand that this is happening because String and Boolean are objects whereas true, false and 'string' are literals.
function foo(bar){
/* if statement here{
I want the code between these two lines to execute when and only when foo is true,
or new Boolean(true), which operator to use?
} */
}
new Boolean(). These objects don't act like booleans.new Boolean(false)is truthy.x == trueis needed. If you are checking for truthiness,xsuffices; if you want to see if it's exactlytrue, you need to writex === true.xdoesn't work at all if you usenew Boolean, since bothnew Boolean(true)andnew Boolean(false)are truthy.Boolean, 2) if you use primitive boolean, don't usex == true.typeof bar === "boolean" && barisn't it the same asbar === true??