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.
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.
Explicitly checking truthiness is better than implicitly checking, and to do that you need to use strict equality true/false, as all other checks are implicit by means of coercing a value to a bool.
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.
I agree. Lodash is totally unnecessary when bundle size matters. Invest more time in learning how specific functions work to save time and resources down the road. Nonetheless, Lodash and jQuery will always have a special place in my heart.
Lodash and Underscore are two of my common pet peeves. In fact, I was just talking to our team about this last week, because we are porting over old code and I specifically said that I didn't wanna be dragging over all the old Lodash stuff, just because it's an easy copy-n-paste.
Those packages were awesome - 10 years ago. Now, they're mostly superfluous. And they serve as crutches for those who haven't bothered to learn modern syntax.
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.
I agree with a bit of this. Sometimes it's simple enough to use forEach or map or reduce directly. And honestly, I wasn't familiar with the spread syntax, and that is 🔥
But there is a bit of a strawman argument here. I don't care about cross-browser compatibility (or browser download size so much at the moment given that I'm doing a lot of Node.js). You've skipped past many of the conveniences of lodash which make it quite worthwhile:
The ability to chain multiple functions together nicely
You wrote a partition alternative, but, please, that is so ugly. Why should we spend our time writing that and struggle getting it right?
When using map or each or filter, there is so much convenience is being able to use the simpler forms like _.map(arr, 'foo.bar.bar') or .filter(arr, {key: value}).
It makes working with enumerating over objects simpler than plain JS
Just in general, it's nice to have around to do the simple things (like partition, but much more of course) that I shouldn't be spending my time worrying about. And it provides a consistent interface to it all. Your examples about type inference and checking just make me ❤️ lodash even more because, just look at those JS examples. It's a such mismash! 😃
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.
Do you think maybe we'll always need something like Babel? So it won't go away.
There are always going to be legacy browsers and users who have to update. When they've all caught up to using ES6 in a few years, by then there is some newer syntax they don't have support for. So you going to need to transpile down to ES6 or ES7, rather than to pre-ES6.
Babel will be required for users on older browsers that don't support bleeding edge. Es6 and even es7 sugar is no longer transpired unless specifically configured. Null coalesce and optional chaining do get transpiled.
We're a place where coders share, stay up-to-date and grow their careers.
Please don't recommend Boolean as boolean type check.
With your approach, you'd incorrectly conclude that a variable of
falsepassed to Boolean() is not a boolean.Boolean will cast to boolean.
Since non-zero numbers are truthy, they will give you a response of true even though not boolean type.
You should use
Bonus points if use you TypeScript
Explicitly checking truthiness is better than implicitly checking, and to do that you need to use strict equality true/false, as all other checks are implicit by means of coercing a value to a bool.
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
trueand 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!!aExplicit checking requires strict equals, in my previous comment, but you will need to test for
trueandfalse.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.I agree. Lodash is totally unnecessary when bundle size matters. Invest more time in learning how specific functions work to save time and resources down the road. Nonetheless, Lodash and jQuery will always have a special place in my heart.
Yep, +1
Lodash and Underscore are two of my common pet peeves. In fact, I was just talking to our team about this last week, because we are porting over old code and I specifically said that I didn't wanna be dragging over all the old Lodash stuff, just because it's an easy copy-n-paste.
Those packages were awesome - 10 years ago. Now, they're mostly superfluous. And they serve as crutches for those who haven't bothered to learn modern syntax.
Yep, 100% agree
Try indent your code so your bullet points number correctly
Below
You can indent 4 spaces in addition to code fence block.
Result
A
B
I agree with a bit of this. Sometimes it's simple enough to use
forEachormaporreducedirectly. And honestly, I wasn't familiar with the spread syntax, and that is 🔥But there is a bit of a strawman argument here. I don't care about cross-browser compatibility (or browser download size so much at the moment given that I'm doing a lot of Node.js). You've skipped past many of the conveniences of
lodashwhich make it quite worthwhile:partitionalternative, but, please, that is so ugly. Why should we spend our time writing that and struggle getting it right?maporeachorfilter, there is so much convenience is being able to use the simpler forms like_.map(arr, 'foo.bar.bar')or.filter(arr, {key: value}).Just in general, it's nice to have around to do the simple things (like
partition, but much more of course) that I shouldn't be spending my time worrying about. And it provides a consistent interface to it all. Your examples about type inference and checking just make me ❤️ lodash even more because, just look at those JS examples. It's a such mismash! 😃Thanks for the post!
Do you think maybe we'll always need something like Babel? So it won't go away.
There are always going to be legacy browsers and users who have to update. When they've all caught up to using ES6 in a few years, by then there is some newer syntax they don't have support for. So you going to need to transpile down to ES6 or ES7, rather than to pre-ES6.
Babel will be required for users on older browsers that don't support bleeding edge. Es6 and even es7 sugar is no longer transpired unless specifically configured. Null coalesce and optional chaining do get transpiled.