10 Amazing JavaScript One Liners - SHOULD YOU Use Them?

Sdílet
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

Komentáře • 40

  • @NickyTheRook
    @NickyTheRook Před rokem +7

    For number10 toSpliced exists now. A copying version of splice (so it doesn't mutate)

    • @JamesQQuick
      @JamesQQuick  Před rokem +1

      Oh really? When did that become available?

    • @NickyTheRook
      @NickyTheRook Před rokem

      @@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.

    • @JamesQQuick
      @JamesQQuick  Před rokem

      Sweet! That's good to know. Thanks for sharing that!!@@NickyTheRook

  • @mgcmsn
    @mgcmsn Před rokem +1

    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.

  • @neontuts5637
    @neontuts5637 Před rokem

    Thanks for sharing your thoughts James. The binary to decimal conversion of 1100000101 is 773.

  • @karlstenator
    @karlstenator Před rokem +1

    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());

    • @melodium10
      @melodium10 Před 11 měsíci +1

      are there any use cases for this? i mean, use cases where you need to capitalize every word in a sentence.

    • @karlstenator
      @karlstenator Před 11 měsíci

      @@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.

  • @deatho0ne587
    @deatho0ne587 Před rokem

    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.

  • @nosthrillz
    @nosthrillz Před rokem

    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?

    • @JamesQQuick
      @JamesQQuick  Před 11 měsíci

      Thanks for sharing all of these options!

  • @seanmills3502
    @seanmills3502 Před rokem +3

    Can you do a video on JavaScript pickup lines?
    Girl, are you CSS? CUZ YOU’VE GOT CLASS. 😛

    • @JamesQQuick
      @JamesQQuick  Před rokem

      Lol very interesting idea

    • @melodium10
      @melodium10 Před 11 měsíci +1

      "Girl, are you element.style.setProperty? because you change my personality variable depending upon the situation"
      terrible because im the one created it

  • @tomcrooks1627
    @tomcrooks1627 Před rokem

    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.

  • @BokoMoko65
    @BokoMoko65 Před rokem

    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.

  • @developerpranav
    @developerpranav Před 11 měsíci

    101000010
    Love to use some oneliners without creating a function!

  • @fun2rideadventure
    @fun2rideadventure Před rokem

    What about for the point 5.....the new function from JS arr.toSorted() ?

  • @binrueiyang2197
    @binrueiyang2197 Před rokem

    Thanks!

  • @soniablanche5672
    @soniablanche5672 Před rokem

    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);

  • @likandokayombo
    @likandokayombo Před rokem

    Thank you for this video

  • @SRG-Learn-Code
    @SRG-Learn-Code Před rokem +1

    😎Dark mode please 😎

  • @MusadiqKhan
    @MusadiqKhan Před 11 měsíci +2

    Wouldn't write a Javascript function to capitalize when css can do the job.

    • @flexairz
      @flexairz Před 9 měsíci

      text-transform: capitalize;

  • @krtirtho
    @krtirtho Před rokem

    We've a one liner library already & that's lodash. Why re-write all the same thing?

  • @adarfou3502
    @adarfou3502 Před rokem +1

    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!

    • @JamesQQuick
      @JamesQQuick  Před rokem

      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.

    • @adarfou3502
      @adarfou3502 Před 11 měsíci

      @@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.

    • @BananStone
      @BananStone Před 11 měsíci

      I had Avery similar experience. Nice to know I'm not alone.

  • @rsroyalrahul5
    @rsroyalrahul5 Před rokem

    lodash
    lodash
    lodash
    lodash
    lodash