I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
Assuming your comment was on my previous comment and not the TypeScript one, as TypeScript will check type is a boolean for you using myVar: Boolean.
Use this to check for type boolean:
typeofmyValue==='boolean'
Use this to check if a value is actually true and boolean, not just truthy. Because triple equals sign does a type check
myVar===true
1===true// false
Beware the double equals sign which coerces ie is checks for truthiness and also handles strings funny.
5=="5"// true0==false// true
I don't see a common reason to use Boolean().
It adds change in logic here so is verbose.
// this checks for truthiness twice if(Boolean(myVar)){}// and equivalent to just this which checks once.if(myVar){}
If you want to be explicit in logic flows use triple equals and a value.
// true for 1. and false for 0 and 1000 and any non Number typeif(myVar===1){}// true for true only and false for everything else.if(myVar===true){}
You could use Boolean() to convert to a Boolean type but I can't remember needing to do this in JS.
varmyNumber=1// or 1000 or 0 or -999 etc.varmyBool=Boolean(myNumber)// can be true. Or false for myNumber equal to zerovara=Boolean("my string")// truevarb=Boolean("")// falseb===false// true
I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
Yes if you have user input you could pass non accepted type to a function. That is a risk for all types and not just boolean and there are a few ways to do that.
Like myVar as Boolean.
Or if your input can't be trusted, then add some sanitization on your form handling or use a check before or within the function as typeof myvar === 'boolean.
Like I said earlier, !!prop and Boolean(prop) are fundamentally the same - they determine the truthiness of a value by coercion, not if it is in fact a boolean, just that it has a value other than "", null, undefined, false and 0. Given an array and object share the same super type object, truthiness check will always return true.
typeof myvar === boolean isn't required when you can just do prop === true || prop === false and explicitly check the values, rather than the type.
Lodash explicitly checks the truthiness of the var, and some other checking for arrays and objects. My suggestion isn't a hard and fast rule, it's just one of many options and it's a useful shorthand check. Feel free to use !!var, Boolean(var) or copy+paste the lodash code.
I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
Ok thanks. My original point was not on the best way to do truthiness though but the fact that these two are not interchangeable as you had suggested, so was recommending it be fixed.
_.isBoolean(true);//trueBoolean(true);//true - same as above line
_.isBoolean(false);//trueBoolean(false);//false. But different result to above line and therefore not equivalent
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Assuming your comment was on my previous comment and not the TypeScript one, as TypeScript will check type is a boolean for you using
myVar: Boolean
.Use this to check for type boolean:
Use this to check if a value is actually
true
and boolean, not just truthy. Because triple equals sign does a type checkBeware the double equals sign which coerces ie is checks for truthiness and also handles strings funny.
I don't see a common reason to use
Boolean()
.It adds change in logic here so is verbose.
If you want to be explicit in logic flows use triple equals and a value.
You could use
Boolean()
to convert to a Boolean type but I can't remember needing to do this in JS.Boolean docs
Relying on TS to ensure type checking is fraught with danger esp with input data. TS isn't a runtime environment, it's just a transpiler.
Boolean(a)
is equivalent to!!a
Explicit checking requires strict equals, in my previous comment, but you will need to test for
true
andfalse
.Okay thanks. I didn't understand your TS before.
Yes if you have user input you could pass non accepted type to a function. That is a risk for all types and not just boolean and there are a few ways to do that.
Like
myVar as Boolean
.Or if your input can't be trusted, then add some sanitization on your form handling or use a check before or within the function as
typeof myvar === 'boolean
.Like I said earlier,
!!prop
andBoolean(prop)
are fundamentally the same - they determine the truthiness of a value by coercion, not if it is in fact a boolean, just that it has a value other than""
,null
,undefined
,false
and0
. Given an array and object share the same super typeobject
, truthiness check will always returntrue
.typeof myvar === boolean
isn't required when you can just doprop === true || prop === false
and explicitly check the values, rather than the type.Lodash explicitly checks the truthiness of the var, and some other checking for arrays and objects. My suggestion isn't a hard and fast rule, it's just one of many options and it's a useful shorthand check. Feel free to use
!!var
,Boolean(var)
or copy+paste the lodash code.Ok thanks. My original point was not on the best way to do truthiness though but the fact that these two are not interchangeable as you had suggested, so was recommending it be fixed.