10 Amazing JavaScript One Liners - SHOULD YOU Use Them?
Vložit
- čas přidán 18. 08. 2024
- Would you use these 1-liners in your JavaScript projects? Let's examine these 10 JavaScript one liners from a recent freeCodeCamp article. Make sure to share your own opinions in the comments!
Read the full article - www.freecodeca...
*DISCORD*
Join the Learn Build Teach Discord Server 💬 - / discord
*STAY IN TOUCH 👋*
Newsletter 🗞 - www.jamesqquic...
Follow me on Twitter 🐦 - / jamesqquick
Check out the Podcast - compressed.fm/
Courses - jamesqquick.co...
*QUESTIONS ABOUT MY SETUP*
Check out my Uses page for my VS Code setup, what recording equipment I use, etc. www.jamesqquic...
*TIMESTAMPS*
00:00 Introduction
01:32 capitalize text
03:11 calculating percent
05:40 remove duplicate elements
08:12 sort elements by property
10:43 check if arrays and objects are equal
12:57 count number of occurrences
15:38 insert an element at a certain position
16:55 Conclusion
For number10 toSpliced exists now. A copying version of splice (so it doesn't mutate)
Oh really? When did that become available?
@@JamesQQuick There are a few of those methods that are fairly new. I don't know the exact date, but they're in the ECMAscript spec right now and all evergreen browsers already have an implementation.
Sweet! That's good to know. Thanks for sharing that!!@@NickyTheRook
I prefer to spend extra time optimizing my utility functions. I also like to create tests for every edge case I can think of. Usually my utilities use the simplest syntax, look verbose and low-level, not one-liners.
However, these one-line cheat sheets are very convenient for prototyping, so I will definitely use them when creating an MVP.
Thanks for sharing your thoughts James. The binary to decimal conversion of 1100000101 is 773.
NOICE! Thanks for sharing!
Here's a variant on Capitalise first letter of text that addresses every letter after a space.
const capitalize = (str) =>
str.replace(/(^|\s)([a-z])/g, (match, p1, p2) => p1 + p2.toUpperCase());
are there any use cases for this? i mean, use cases where you need to capitalize every word in a sentence.
@@melodium10 perhaps for a Title or a Subtitle - ie Title Case - However there are rules to adhere to as not all words should have their first letter capitilised within a Title.
I might but it is for the one-timers that are barely needed. Functions that a more library I want to test, it be easy to read and take in options.
6. Check if objects are equal will not work with stringify if the object keys are in a different order.
To do that comparison, there are some options, but not ideal :)
- compare stringified objects where they have already been sorted by key (const sortedObj = (obj) => Object.keys(obj).sort((a,b) => a.localeCompare(b)).map(k => ({[k]: obj[k]})))
- look at Object.keys length between the two initially. if the same, check that every key and value is also on the second object
7. instead of reduce you can filter for items that fit the value, then return length :)
10. you can also copy the array, splice it and return it for more readable code?
Thanks for sharing all of these options!
Can you do a video on JavaScript pickup lines?
Girl, are you CSS? CUZ YOU’VE GOT CLASS. 😛
Lol very interesting idea
"Girl, are you element.style.setProperty? because you change my personality variable depending upon the situation"
terrible because im the one created it
To count the number of occurrences in an array I usually filter the array and check it’s length, seems simpler than using a reduce.
Totally valid way to do it!
1010000101
Percentage calculations make more sense if we use decimal numbers. For instance, 70% actually is 70 /100 = 0.7
The % thing is just a formatting tool to ease up the reading. The % symbol does mean "per cent", or "per hundred".
So, I prefer to store the "percentage" as decimal and format accordingly whenever it's needed.
Also, for monetary applications, I recommend storing the values in cents and use only fixed point math. It's way more efficient.
101000010
Love to use some oneliners without creating a function!
What about for the point 5.....the new function from JS arr.toSorted() ?
Thanks!
Wow, thank you!!
removeDuplicates will not work on objects though, Set will use === to compare items to see if they are duplicates so unless your goal is to remove objects that have the same reference then you'll have to write your own removeDuplicates function.
By the way, NodeJS has a promise version of setTimeout
const { setTimeout: setTimeoutPromise } = require('node:timers/promises');
...
await setTimeoutPromise(1000);
Thank you for this video
Thanks for watching :)
😎Dark mode please 😎
Wouldn't write a Javascript function to capitalize when css can do the job.
text-transform: capitalize;
We've a one liner library already & that's lodash. Why re-write all the same thing?
Nothing wrong with that!
Please don't publicise Reed Barger. His courses are conning fresh learners out of money and he has an absolutely horrific approach to customer service. I can't understand how he's weasled his way into free code camp and now scrimba too!
Do you have any details to clarify that? I don't know anything about him personally, but if you have specific references to what you're saying, I'd love to learn more.
@@JamesQQuick I had accessibility issues with his learn react course and when I raised them I was either ignored or told I wasn't using it properly and insulted. He has absolutely no regard for anyone with accessibility issues. I was so disappointed to see him listed as a scrimba instructor.
I had Avery similar experience. Nice to know I'm not alone.
lodash
lodash
lodash
lodash
lodash
haha that works too!