37 Common JavaScript Utility Functions for String, Object, Array & Date
Utility functions are like shortcuts for your code. They let you handle things like arrays, dates, and strings without writing a lot of extra code. Instead of doing the same tasks over and over, you can use these functions to get the job done faster, consistent and prevent errors.
In this article, we’ll look at 37 common JavaScript utility functions that every developer should know. If you’re new to JavaScript or haven’t documented your own utility functions, this article is for you. By learning and using these utilities, you’ll save time, reduce errors, and gain a deeper understanding of how to write clean, reusable code.
Related posts
- 20 Lesser-known javascript features You probably dont know
- Javascript Array/Object Desctructurung Explained
So, let’s get started and see how these utilities can make your JavaScript coding easier and more effective.
Common String Utils in Javascript
1. Capitalize
Capitalizes the first letter of a string.
2. Convert to Camel Case
Converts a string with hyphens to camel case (camelCase).
3. Convert to Kebab Case
Converts a string to kebab case (kebab-case).
4. Convert to Snake Case
Converts a string to snake case (snake_case).
5. Convert to Pascal Case
Converts a string to pascal case (PascalCase).
6. Check if String Contains Another String
Checks if a string contains a specified substring.
7. Replace All Occurrences
Replaces all occurrences of a substring within a string.
8. Count Occurrences of a Character
Counts the number of times a character appears in a string.
9. Escape HTML
Escapes HTML special characters to prevent XSS attacks.
10. Unescape HTML
Unescapes HTML special characters.
11. Format Money
Formats a number as a currency string, with commas as thousands separators.
12. Truncate Paragraphs
Truncates a paragraph to a specified length and adds an ellipsis if truncated.
If you want truncate string for styling purpose, you can do this with Tailwindcss truncate / line-clamp
Common Object Utils in Javascript
13. Check if a value is an object
Checks if a value is an object.
14. Merge Objects (Flat object)
Merges two or more objects into one.
15. Deep Merge Objects
Deeply merges two or more objects.
16. Deep Clone object
Creates a deep clone of an object.
Check The Right Way to Clone Nested Object/Array in Javascript for more advanced use cases.
17. Omit / Delete keys from object
Returns a new object with specified keys omitted.
18. Pick keys to create new object
Returns a new object with only the specified keys.
19. Invert Object keys and values
Inverts the keys and values of an object.
20. Check if object is empty
Checks if an object is empty (has no enumerable properties).
21. Map Object
Creates a new object with the same keys but with values transformed by a function. Similar to array.map()
, but for object
22. Filter object
Creates a new object with only the properties that pass a filter function. Similar to array.filter()
, but for object
23. Freeze Object
Freezes an object, making it immutable.
24. Deep Freeze Object
Deeply freezes an object, making it and all nested objects immutable.
Common Array Utils in Javascript
25. Group Array Based on specified function
Groups the elements of an array based on a specified function.
26. Array Intersection
Returns the intersection of two arrays.
27. Zip Arrays
Combines two arrays into an array of pairs.
28. Remove duplicate item
Removes duplicate values from an array.
29. Split array
Splits an array into chunks of specified size.
30. Array difference
Returns the difference between two arrays.
Common Date Utils in Javascript
These utility functions can help you perform various date manipulations and formatting tasks in JavaScript efficiently.
31. Format date without library
Formats a date into a specified string format.
32. Add days
Adds a specified number of days to a date.
33. Substract days
Subtracts a specified number of days from a date.
34. Count days between 2 dates
Calculates the number of days between two dates.
35. Check if a date is weekend
Checks if a given date falls on a weekend.
36. Count days of a month
Gets the number of days in a specific month of a specific year.
37. Duration between 2 dates
For more advance use-cases and tested utils for Date, consider using date library like day.js
Conclusion
We’ve covered 37 useful JavaScript utility functions that can help you code more efficiently. These functions simplify tasks like array handling, date formatting, and string manipulation. Using them can save you time and reduce errors in your code.
If this article was helpful, please share it with others who might benefit from it. Bookmark it for future reference, and if you have any favorite utility functions that weren’t mentioned, feel free to share them in the comments.
Happy coding!