Education logo

JavaScript Quick Functions: Simplify Your Coding with Reusable Methods

JavaScript Quick Functions: Simplify Your Coding with Reusable Methods

By Rahul SharmaPublished about a year ago 3 min read
JavaScript Quick Functions: Simplify Your Coding with Reusable Methods

Most of you probably already know them, lodash/underscore was built to provide utility functions for common tasks in Javascript. But some of you don’t want to install a package for a small use case. So I created this file to provide quick access to the most used functions. I hope you will find it useful.

Functions

Functions are reusable blocks of code that perform specific tasks. In JavaScript, functions can be defined in different ways:

Regular Function

A regular function is defined using the function keyword followed by the function name and a set of parentheses containing the function parameters.

Regular Function

Function Expression

A function expression is a way to define a function as a variable assignment. It uses the const keyword to assign an anonymous function to a variable.

Function Expression

Arrow Function

Arrow functions provide a concise syntax for writing functions. They are defined using the arrow (=>) notation.

Arrow Function

Generator Function

A generator function is a special type of function that can be paused and resumed. It is defined using the function* syntax.

Generator Function

Time the execution of your code

Time the execution of your code

Evaluate a string

To evaluate a string as JavaScript code in JavaScript, you can use the eval() function. The eval() function takes a string argument and treats it as code, executing it within the current execution context.

Evaluate a string

Creating an Array of Numbers from 1 to n

Creating an Array of Numbers from 1 to n

Create an array of numbers from 1 to n with a step

Create an array of numbers from 1 to n with a step

Shuffling an array

you may need to randomize the order of elements in an array. This can be useful for creating games, conducting surveys, or any situation that requires a randomized sequence.

Shuffling an array

Converting an Object into a List of [key, value] Pairs in JavaScript

you may often need to convert them into a list of [key, value] pairs. This transformation can be helpful for various purposes, such as iterating over object properties or manipulating the data in a different format.

Converting an Object into a List of [key, value] Pairs in JavaScript

Convert a list of [key, value] pairs into an object

Convert a list of [key, value] pairs into an object

Remove Duplicated from Array

By utilizing a Set object, duplicates are automatically eliminated because a set can only contain unique values. By converting the set back into an array using the spread operator, we obtain a new array that retains the original order of elements but without any duplicates.

Remove Duplicated from Array

Remove an element from the array

Remove an element from the array

Merge 2 Arrays

Merging arrays is a common operation in JavaScript, and there are multiple methods to achieve it. You can use the concat method, the spread operator, or the push() method to combine two arrays into a single array. Depending on your specific use case and preferences, choose the method that best suits your needs.

Merge 2 Arrays

Generate random number

By utilizing the Math.random() function along with mathematical operations, we can easily generate random numbers within the desired range. This method provides a simple and efficient solution for various applications, such as game development, simulations, and random data generation.

Generate random number

Find the factorial of a number

By using a recursive approach, we can calculate the factorial of a number efficiently. The recursive factorial function breaks down the problem into smaller subproblems, ultimately leading to the desired result. Understanding this approach is essential for solving factorial-related problems and implementing mathematical algorithms.

Find the factorial of a number

Find the sum of an array

Find the sum of an array

Find largest numbers

The Math.max() method can be used to find the largest number among a set of values. We can combine this method with the spread operator (...) to find the largest number in an array.

Find largest numbers

Find smallest numbers

The Math.min() method can be used to find the smallest number among a set of values. We can combine this method with the spread operator (...) to find the small number in an array.

Find smallest numbers

Thank you for taking the time to read about "React best practices and patterns to reduce code". If you have any questions or comments, please feel free to leave them below. We would love to hear your thoughts on this topic and help you in any way we can. Let's continue the conversation!

Must Read If you haven't

Catch me on YouTube, Github, LinkedIn, Medium, Stackblitz, and Blogspot. I share a lot of content related to these topics, including tutorials, articles, and code samples. Don't hesitate to follow or reach out to me on any of these platforms if you have any questions or want to connect.

teacherinterviewhow todegreecoursescollege

About the Creator

Rahul Sharma

I'm a technology enthusiast who does web development. Passionate to contribute to open-source projects and making cool products.

✔ I’m currently learning about Flutter

✔ Ask me anything you want, If I'm alive I will answer within seconds 😉

Enjoyed the story?
Support the Creator.

Subscribe for free to receive all their stories in your feed. You could also pledge your support or give them a one-off tip, letting them know you appreciate their work.

Subscribe For Free

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

    RSWritten by Rahul Sharma

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.