DeveloperBreeze

Introduction

JavaScript utility libraries are essential tools that help developers perform common tasks more efficiently, reduce boilerplate code, and improve code readability. These libraries offer a wide range of utility functions for tasks such as array manipulation, object handling, data transformation, and more. This cheatsheet provides a quick reference to some of the most popular JavaScript utility libraries and their key features.

1. Lodash

Lodash is one of the most popular JavaScript utility libraries, offering a wide range of functions for common programming tasks such as working with arrays, objects, and strings.

<table>
  <tr>
    <th>Function</th>
    <th>Description</th>
    <th>Example</th>
  </tr>
  <tr>
    <td><code>_.chunk(array, size)
    Splits an array into groups of the specified size.
    _.chunk(['a', 'b', 'c', 'd'], 2) => [['a', 'b'], ['c', 'd']]
  
  
    _.debounce(func, wait)
    Creates a debounced function that delays invoking the provided function until after the specified wait time.
    _.debounce(() => console.log('Hello'), 1000)
  
  
    _.cloneDeep(value)
    Creates a deep clone of the provided value.
    _.cloneDeep({ a: 1, b: { c: 2 } }) => { a: 1, b: { c: 2 } }
  
  
    _.merge(object, sources)
    Merges two or more objects into one, combining their properties.
    _.merge({ a: 1 }, { b: 2 }) => { a: 1, b: 2 }
  
  
    _.uniq(array)
    Creates a duplicate-free version of an array.
    _.uniq([1, 2, 2, 3, 4, 4]) => [1, 2, 3, 4]
  

2. Underscore.js

Underscore.js is a lightweight utility library similar to Lodash, providing useful functions for working with arrays, objects, and other data types.

<table>
  <tr>
    <th>Function</th>
    <th>Description</th>
    <th>Example</th>
  </tr>
  <tr>
    <td><code>_.each(list, iteratee)
    Iterates over a list, invoking the iteratee for each element.
    _.each([1, 2, 3], alert)
  
  
    _.map(list, iteratee)
    Creates a new array by applying the iteratee to each element in the list.
    _.map([1, 2, 3], num => num * 3) => [3, 6, 9]
  
  
    _.reduce(list, iteratee, memo)
    Reduces a list to a single value by iterating and combining elements.
    _.reduce([1, 2, 3], (sum, num) => sum + num, 0) => 6
  
  
    _.filter(list, predicate)
    Returns an array of elements that pass a truth test.
    _.filter([1, 2, 3, 4], num => num % 2 == 0) => [2, 4]
  
  
    _.findWhere(list, properties)
    Returns the first element that matches the specified properties.
    _.findWhere([{a: 1}, {a: 2}], {a: 2}) => {a: 2}
  

3. Moment.js

Moment.js is a popular library for parsing, validating, manipulating, and formatting dates in JavaScript.

<table>
  <tr>
    <th>Function</th>
    <th>Description</th>
    <th>Example</th>
  </tr>
  <tr>
    <td><code>moment().format(formatString)
    Formats a date as a string according to the specified format.
    moment().format('MMMM Do YYYY, h:mm:ss a') => September 20th 2024, 3:45:07 pm
  
  
    moment().add(number, unit)
    Adds a specified amount of time to a date.
    moment().add(7, 'days') => Moment object 7 days in the future
  
  
    moment().subtract(number, unit)
    Subtracts a specified amount of time from a date.
    moment().subtract(1, 'year') => Moment object 1 year ago
  
  
    moment().fromNow()
    Displays the time from now in a human-readable format.
    moment('2020-01-01').fromNow() => 3 years ago
  
  
    moment().diff(moment, unit)
    Calculates the difference between two dates.
    moment().diff(moment('2000-01-01'), 'years') => 24
  

4. Ramda

Ramda is a functional programming library for JavaScript developers, designed for better code composition and function handling.

<table>
  <tr>
    <th>Function</th>
    <th>Description</th>
    <th>Example</th>
  </tr>
  <tr>
    <td><code>R.compose(...functions)
    Composes functions from right to left.
    R.compose(Math.abs, R.add(1))(-5) => 4
  
  
    R.curry(fn)
    Returns a curried version of the provided function.
    R.curry((a, b) => a + b)(1)(2) => 3
  
  
    R.filter(predicate, list)
    Returns a new list containing only the elements that satisfy the predicate.
    R.filter(x => x % 2 === 0, [1, 2, 3, 4]) => [2, 4]
  
  
    R.map(fn, list)
    Applies the function to each element of the list.
    R.map(x => x * 2, [1, 2, 3]) => [2, 4, 6]
  
  
    R.reduce(reducer, initialValue, list)
    Reduces the list to a single value using the reducer function.
    R.reduce(R.add, 0, [1, 2, 3]) => 6
  

5. Date-fns

Date-fns is a modern JavaScript date utility library that provides over 200 functions for manipulating dates without modifying native JavaScript objects.

<table>
  <tr>
    <th>Function</th>


 <th>Description</th>
    <th>Example</th>
  </tr>
  <tr>
    <td><code>format(date, formatString)
    Formats the date according to the provided format string.
    format(new Date(), 'MMMM do, yyyy') => 'September 20th, 2024'
  
  
    addDays(date, amount)
    Returns a new date with the specified number of days added.
    addDays(new Date(), 10) => Date object 10 days in the future
  
  
    differenceInDays(dateLeft, dateRight)
    Returns the number of days between two dates.
    differenceInDays(new Date('2024-09-20'), new Date('2024-09-10')) => 10
  
  
    isBefore(date, dateToCompare)
    Checks if the first date is before the second one.
    isBefore(new Date('2024-09-10'), new Date('2024-09-20')) => true
  
  
    parse(dateString, formatString, referenceDate)
    Parses a date string according to the provided format string.
    parse('20th September 2024', 'do MMMM yyyy', new Date()) => Date object for 20th September 2024
  

Conclusion

This cheatsheet covers some of the most popular JavaScript utility libraries, providing a quick reference to key functions and their usage. These libraries help streamline your development process, reduce code duplication, and enhance the functionality of your JavaScript applications.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

  const multiply = (a, b) => a * b;
  console.log(multiply(4, 7)); // 28
  • Primitives hold a single value and are immutable.
  • Non-primitives hold collections or behaviors and are mutable.

Dec 11, 2024
Read More
Tutorial
javascript

JavaScript in Modern Web Development

  • JavaScript is used in IoT devices for controlling hardware and sensors.
  • Example: Node.js-based IoT applications.
  • Libraries like Three.js and Babylon.js enable building browser-based games.
  • Example: Interactive 3D experiences.

Dec 10, 2024
Read More
Tutorial
javascript

Advanced JavaScript Tutorial for Experienced Developers

async function processData() {
    try {
        const data = await fetchData();
        console.log(data);
    } catch (error) {
        console.error('Error:', error); // Output: Error: Error fetching data!
    }
}

processData();

The Event Loop is a core concept in JavaScript that allows the language to perform non-blocking I/O operations despite being single-threaded. Understanding how the Event Loop works is crucial for mastering asynchronous programming.

Sep 02, 2024
Read More
Tutorial
javascript

JavaScript Tutorial for Absolute Beginners

JavaScript is often used to add interactivity to web pages by responding to events like clicks, mouse movements, and form submissions.

<button id="myButton">Click Me!</button>

<script>
  document.getElementById("myButton").addEventListener("click", function() {
    alert("Button clicked!");
  });
</script>

Sep 02, 2024
Read More
Tutorial
javascript

Creating a Dropdown Menu with JavaScript

To follow along with this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript. Familiarity with DOM manipulation in JavaScript will be helpful but isn’t required.

The first step in creating a dropdown menu is to build the basic HTML structure. We’ll create a navigation bar with a dropdown menu that is initially hidden.

Sep 02, 2024
Read More
Tutorial
javascript

Understanding call, apply, and bind in JavaScript

In this example, bind ensures that this inside handleClick refers to user when the button is clicked.

  • call: Invokes the function immediately with a specified this value and individual arguments.
  • apply: Invokes the function immediately with a specified this value, but arguments are passed as an array.
  • bind: Returns a new function with a specified this value and optional pre-set arguments, but does not invoke the function immediately.

Aug 30, 2024
Read More
Tutorial
javascript

Creating a Component Library with Storybook and React

src/
└── components/
    ├── Button/
    │   ├── Button.jsx
    │   ├── Button.css
    │   └── Button.stories.jsx
    └── Input/
        ├── Input.jsx
        ├── Input.css
        └── Input.stories.jsx

Once you’ve developed a set of components, you might want to share them with others or reuse them across different projects. You can do this by publishing your component library to npm.

Aug 27, 2024
Read More
Tutorial
solidity

Building a Decentralized Application (DApp) with Smart Contracts

To interact with the Ethereum network, you need to connect MetaMask to your DApp.

Steps to Connect MetaMask:

Aug 22, 2024
Read More
Cheatsheet

CSS-in-JS Libraries Cheatsheet

  • Familiar CSS syntax with additional JS power.
  • Strong community and ecosystem support.
  • Supports server-side rendering and theming.
  • Can introduce overhead with large stylesheets.
  • May require Babel configuration for optimal usage.

Aug 21, 2024
Read More
Cheatsheet

Comprehensive React Libraries Cheatsheet

No preview available for this content.

Aug 21, 2024
Read More
Cheatsheet

Responsive Design Frameworks Cheatsheet

  • Bootstrap: Best for fast development with lots of components.
  • Foundation: Good for scalable, enterprise-level apps.
  • Bulma: Lightweight and easy to learn.
  • Tailwind CSS: Highly customizable with utility-first approach.
  • Materialize: Clean Material Design look.
  • Semantic UI: Human-friendly HTML with good customization.

Evaluate the requirements of your project and choose the framework that best fits your workflow and design goals.

Aug 21, 2024
Read More
Cheatsheet

Front-End Development Tools and Libraries Cheatsheet

Front-end development has evolved significantly, with a plethora of tools and libraries available to enhance productivity and build responsive, interactive web applications. This cheatsheet covers the most essential and popular tools and libraries for front-end developers, organized by category.

This cheatsheet offers a quick reference to some of the most widely-used tools and libraries in front-end development. Whether you're building simple websites or complex web applications, these resources can help streamline your workflow and improve your productivity. Explore these tools, experiment with them, and find the ones that best fit your development style and project needs.

Aug 21, 2024
Read More
Tutorial
javascript

Leveraging Machine Learning Models in Real-Time with TensorFlow.js and React: Building AI-Powered Interfaces

This installs TensorFlow.js, which includes tools for loading models, performing predictions, and managing tensors.

Before we dive into integrating TensorFlow.js with React, it’s important to understand the basics of TensorFlow.js.

Aug 20, 2024
Read More
Code
javascript

Calculating the Average of an Array in JavaScript

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Date Manipulation and Sum Calculation

No preview available for this content.

Jan 26, 2024
Read More
Code
php

Date Formatting for Specific Date ('Y-m-d')

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

POST Request with Fetch API and JSON Data

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!