<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p" -->

---
title: JavaScript One-Liners to Use in Every Project | daily.dev
description: Learn essential one-liners in JavaScript for tasks like capitalizing text, calculating percent, getting a random element, removing duplicates, sorting...
canonical: https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: JavaScript One-Liners to Use in Every Project | daily.dev
og:description: Learn essential one-liners in JavaScript for tasks like capitalizing text, calculating percent, getting a random element, removing duplicates, sorting...
og:url: https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p
og:image: https://api.daily.dev/og/posts/kXq3w4e2P.png
og:image:alt: JavaScript One-Liners to Use in Every Project
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript One-Liners to Use in Every Project

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 5 min read · 407 upvotes · 22 comments

## Summary

Learn essential one-liners in JavaScript for tasks like capitalizing text, calculating percent, getting a random element, removing duplicates, sorting elements, checking equality, counting occurrences, waiting for a certain time, plucking property from an array of objects, and inserting an element at a certain position.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/javascript-one-liners-to-use-in-every-project/>

## Community discussion

Top comments from developers on daily.dev.

**@siddiquiaffan** · 13 upvotes

> To capitalize first letter of every word in the string,
> ```js
> const capitalize = (str) => str.split(' ').map(word =>  `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ')
>
> // Edit: Using reegex
> const capitalize = (str) => {
>     return str.replace(/\b[a-z]/gi, char => char.toUpperCase())
> }
> ```

**@bherrero** · 6 upvotes

> Nice article.
> However, JSON.stringify could be problematic when checking objects as you are comparing strings. So if the properties of the object are the same but in different order it won't be equal and it should.
>
> Example:
>
> ```
> const a = { test: 1, hey: 2};
> const b = { hey: 2, test: 1};
>
> isEqual(a, b); // returns false
> ```

**@aldennis** · 5 upvotes

> This is why I keep reading articles I think would have nothing new to me. Never even knew about set and plunk. Plunk is definitely something I am adding to my toolbelt. Thanks.

**@maxvanderschee** · 2 upvotes

> Good one. I started writing my own methods library [@mvdschee/use](https://github.com/mvdschee/use). I keep reusing code snippets like that; it saves a lot of time, not looking up how to do something.

**@uraniumking007** · 1 upvotes

> man that's helpful

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 1 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"JavaScript One-Liners to Use in Every Project","url":"https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p"},"datePublished":"2023-03-10T22:48:01.942Z","dateModified":"2024-04-15T13:21:18.973Z","description":"Learn essential one-liners in JavaScript for tasks like capitalizing text, calculating percent, getting a random element, removing duplicates, sorting...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7b969bed1d3a8a19799dcae66f2457e5?_a=AQAEufR","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7b969bed1d3a8a19799dcae66f2457e5?_a=AQAEufR","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":22,"discussionUrl":"https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":407},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":22}],"keywords":"webdev,javascript","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"JavaScript One-Liners to Use in Every Project"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/javascript-one-liners-to-use-in-every-project-kxq3w4e2p","comment":[{"@type":"Comment","text":"To capitalize first letter of every word in the string,\nconst capitalize = (str) =&gt; str.split(&#x27; &#x27;).map(word =&gt;  `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(&#x27; &#x27;)\n\n// Edit: Using reegex\nconst capitalize = (str) =&gt; {\n    return str.replace(/\\b[a-z]/gi, char =&gt; char.toUpperCase())\n}","datePublished":"2023-03-11T10:30:28.743Z","dateModified":"2023-05-10T05:36:21.803Z","url":"https://daily.dev/posts/kXq3w4e2P#c-i7EgqZnsy","author":{"@type":"Person","name":"Siddiqui Affan","url":"https://daily.dev/siddiquiaffan","image":"https://media.daily.dev/image/upload/f_auto/v1648024173/avatars/X3TQ0Pd91kOeT86d17YEv"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":13}},{"@type":"Comment","text":"Nice article.\nHowever, JSON.stringify could be problematic when checking objects as you are comparing strings. So if the properties of the object are the same but in different order it won’t be equal and it should.\nExample:\nconst a = { test: 1, hey: 2};\nconst b = { hey: 2, test: 1};\n\nisEqual(a, b); // returns false","datePublished":"2023-03-13T04:07:40.403Z","url":"https://daily.dev/posts/kXq3w4e2P#c-wzoC98rUe","author":{"@type":"Person","name":"Borja Herrero","url":"https://daily.dev/bherrero","image":"https://avatars3.githubusercontent.com/u/12747297?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":6}},{"@type":"Comment","text":"This is why I keep reading articles I think would have nothing new to me. Never even knew about set and plunk. Plunk is definitely something I am adding to my toolbelt. Thanks.","datePublished":"2023-03-11T15:39:14.491Z","url":"https://daily.dev/posts/kXq3w4e2P#c-xiUO0uKxs","author":{"@type":"Person","name":"Al Dennis","url":"https://daily.dev/aldennis","image":"https://lh3.googleusercontent.com/a/AEdFTp7laNbtst3aEAemEHDc94c0pZaXcC9ycjXnIvg4=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5}},{"@type":"Comment","text":"Good one. I started writing my own methods library @mvdschee/use. I keep reusing code snippets like that; it saves a lot of time, not looking up how to do something.","datePublished":"2023-03-12T06:52:51.044Z","url":"https://daily.dev/posts/kXq3w4e2P#c-g1laxcPpZ","author":{"@type":"Person","name":"Max van der Schee","url":"https://daily.dev/maxvanderschee","image":"https://avatars.githubusercontent.com/u/26795741?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"man that’s helpful","datePublished":"2023-03-12T05:20:10.023Z","url":"https://daily.dev/posts/kXq3w4e2P#c-ymXNHvLdh","author":{"@type":"Person","name":"Bhavesh Patil","url":"https://daily.dev/uraniumking007","image":"https://lh3.googleusercontent.com/a/AEdFTp7rv_xSI8e5qQPHz8w_ceQtlP9_jSh-Y-0HP7Y85g=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
```

