JavaScript Quick Functions: Simplify Your Coding with Reusable Methods
JavaScript Quick Functions: Simplify Your Coding with Reusable Methods
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,fl_progressive,q_auto,w_1024/6480714f7ac061001c513c9f.png)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648064beab39d1001d4bb854.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648064f3217661001d5db7aa.jpg)
Arrow Function
Arrow functions provide a concise syntax for writing functions. They are defined using the arrow (=>) notation.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648065437ac061001c513c5c.jpg)
Generator Function
A generator function is a special type of function that can be paused and resumed. It is defined using the function* syntax.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648065677ac061001c513c5d.jpg)
Time the execution of your code
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648065da7ac061001c513c61.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/6480663e5233dd001dfa9975.jpg)
Creating an Array of Numbers from 1 to n
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/6480675e5233dd001dfa997d.jpg)
Create an array of numbers from 1 to n with a step
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/648067f3e068e6001df5b6b2.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/6480687c217661001d5db7bf.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806906217661001d5db7c6.jpg)
Convert a list of [key, value] pairs into an object
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806944217661001d5db7c8.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806a2bab39d1001d4bb870.jpg)
Remove an element from the array
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806a92217661001d5db7cf.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806b9ee068e6001df5b6bd.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806c66217661001d5db7d5.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806d19e068e6001df5b6c4.jpg)
Find the sum of an array
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806d5c217661001d5db7d7.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806df07ac061001c513c8b.jpg)
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.
![](https://res.cloudinary.com/jerrick/image/upload/d_642250b563292b35f27461a7.png,f_jpg,q_auto,w_720/64806e4c7ac061001c513c8c.jpg)
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
- React best practices and patterns to reduce code - Part 1
- React best practices and patterns to reduce code - Part 2
- React.js state management using signals
- What, Why, and How Do Javascript Static Initialization Blocks?
- 28 Javascript Array Methods: A Cheat Sheet for Developer
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.
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.
Comments
There are no comments for this story
Be the first to respond and start the conversation.