JavaScript keeps growing and growing, opening doors for new
"to be tech geeks" in the market as it's one of the easiest languages to start. (is it ...
For further actions, you may consider blocking this person and/or reporting abuse
This could be simplified to
because you're not actually toggling the original boolean. I'm not sure it offers any benefit beyond using
!b
in the first place, though.I think that if you're going to use full words elsewhere, like
isArray
you should use it here, and make the functionuniqueArray
. Most of the time, I think if what you're wanting is a set, you should use it as a set. In those cases you probably don't need to convert it back to an array anyway!Except... days aren't always 86400 seconds long, and this will fail when times are close to daylight savings, for example. There's a reason date libraries are complicated :)
Woah, man!
This is really helpful, I appreciate your effort <3
Hi there, really cool one-liners! My favorite is the true type one. I actually use it in almost all of my projects.
I'm not sure about the
isArray
though. This is indeed a one-liner, but in my opinion, it does not add much, besides being a shorter named version of the verboseArray.isArray
. Wrapping a function that does exactly what you want with another function seems overkill to me. Don't you think?Otherwise, great job!
Just use typeof
nope:
An array is an
object
. Whether it's anArray
is a separate question. I suppose iftrueTypeOf
were documented, describing its semantics, it would be clearer. Remember, all, syntax doesn't convey everything. You also need semantics. :)Yeah, sometimes it's handy to just call a function to check, rather than doing
Array.isArray(arr)
you know.Can you explain what this does and where you can put this in practice?
Check out this post for possible case use:
dev.to/cherif_b/using-javascript-t...
Um, why write
trueTypeOf
when you can just use the typeof operator?This is helpful if we need to verify if 'foobar' is a string, but in some cases, we want to get the type instead of checking (verifying).
Awesome article!
A couple suggestions though:
const isTabInView = () => !document.hidden; // Not hidden
and
const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform); // to be a function like other examples
Missed it there, thanks for the suggestions!
Made changes accordingly.
Be careful not to go too crazy with one liners. If you or a team mate has to dig back into the code after 6 months or a couple years, it can take some brain power to parse out what is happening.
If you are using a build process, the code is going to be minified and optimized in ways that are basically illegible to humans.
Code readability is my primary metric for good work.
Agreed 🤞
Hi Muhammad, I think there is an issue with the longhand form of your bonus snippet. If name is null, undefined or an empty string (in fact any value) the variable fullName will be assigned. I think your intention was for fullName only to be assigned when
An alternative form of this condition using De Morgan's Law to invert the logic is...
(notice the initial not operation in the following condition)
Also, there is a good argument here for cohesive-equality because
null == undefined
. The following is equivalent.Best regards.
Thanks for sharing. I always think these 1-liners are useful, so I made this site 1loc.dev.
Hopefully it's useful for you as well.
That's good. But it will return true if we call it with no argument
15 and 16 doesn't work with multi character emojis
You could use Array.from first to convert the string into an array of strings each holding a single character
This will work correctly
Very useful. thank you
Glad you liked it <3
Hello, your article is great. Can I translate it to Chinese community website? I will indicate the source and author.
I would love it 😉
In code reviews, I won't ever call someone a "pro" because he/she writes "one-liners". All the opposite.
Me neither.