Considering you are a JavaScript developer, and spent lot of hours daily with code. Always trying to make your code cleaner, faster, and easier to manage. Always remember that while popular methods like map(), filter(), and reduce() get all the attention, there are many lesser-known JavaScript functions also exists that can save your time and effort both efficiently. These hidden JS methods are often overlooked by us, but once you start using them, they will become important tools in your coding toolkit slowly.
Table of Contents
Letโs explore five of such underrated JS functions, examine how they work with practical examples, and see how they can transform your coding efficiency that you will love and enjoy.
1. Object.fromEntries(): Turning Arrays into Objects
Let’s see you have an array of key-value pairs i mean dictionary, and you need to convert it into an object. Usually, you loop through the array elements and build the object manually. But why you do that when Object.fromEntries() can handle it in a single line code? This method takes an iterable of key-value pairs (like an array of arrays) and returns a new object.
For example, suppose you are working with a list of user preferences stored as [[“theme”, “dark”], [“notifications”, “on”]. Here is the way how you can convert it easily:
const preferences = [["theme", "dark"], ["notifications", "on"]];
const userSettings = Object.fromEntries(preferences);
console.log(userSettings);
// { theme: "dark", notifications: "on" }

This function is valuable when dealing with data from APIs or form submissions. Pair it with Object.entries() (which does the reverse), and you have got a powerful JS method for manipulating object data. It is less common, it is efficient, and it is a very useful method to reduce line of code. Am I Right BRO !
2. Array.flatMap(): Combining Flattening and Mapping
If you needed to map over an array and flatten the result in one go, meet with our another less common JS method Array.flatMap(). This method is a combination of map() and flat() with a depth of 1, making it perfect for scenarios where you are transforming and simplifying nested arrays. Mind it!
Consider a list of categories where each category has sub-items, and you want to create a flat list of all items:
const categories = [
{ name: "JS Basics", items: ["variables", "functions"] },
{ name: "Advanced JS", items: ["promises", "async/await"] }
];
const allItems = categories.flatMap(category => category.items);
console.log(allItems);
// ["variables", "functions", "promises", "async/await"]
This replace using a map() followed by a flat() method, saving lot of time and keeping your code very simple understandable and easy to read. It is ideal for data processing tasks like preparing lists for display or analysis, offering a unique way to handle JS array’s efficiently.
3. String.padStart() and padEnd(): A Perfect Alignment for Every Time
Do you need to format strings to a fixed length, like padding a number with zeros or aligning text? String.padStart() and String.padEnd() are your new best friends. These methods let you add characters to the beginning or end of a string until it reaches your mentioned length.
For instance, if you are formatting invoice numbers to always be five digits use below JS code sample:
const invoiceNumber = "42";
const paddedNumber = invoiceNumber.padStart(5, "0");
console.log(paddedNumber);
// "00042"

Or, for a design purpose, align text for a UI element:
const label = "Sale";
const paddedLabel = label.padEnd(10, ".");
console.log(paddedLabel);
// "Sale......"

These functions are less used but incredibly handy for formatting logs, IDs, or even aesthetic layouts. Their simplicity and versatility make them a fresh addition to your JavaScript arsenal.
4. Promise.any(): Racing to the First Success
When working with multiple promises, you are probably familiar with Promise.all(), which waits for all promises to resolve. But what if you only want to wait for the first one that succeeds? That is where Promise.any() used. It takes an array of promises and returns the first one that executed, do not wait for the rest.
Consider you are fetching data from multiple APIs and want the fastest response:
const promise1 = new Promise((resolve, reject) => setTimeout(() => reject("Slow API 1"), 1000));
const promise2 = new Promise((resolve, reject) => setTimeout(() => resolve("Fast API 2"), 500));
const promise3 = new Promise((resolve, reject) => setTimeout(() => reject("Slow API 3"), 1500));
Promise.any([promise1, promise2, promise3])
.then(value => console.log(value)) // "Fast API 2"
.catch(error => console.log(error));
This is an unique way for improving user experience by prioritizing speed, especially in applications with redundant data sources. It is a fresh perspective on promise handling that can set your content correctly and efficiently.
5. WeakRef.prototype.deref(): Managing Memory Like a Pro
Memory management in JavaScript can be tricky, especially with objects that might no longer be needed. WeakRef and its deref() method allow you to create a weak reference to an object, which doesnโt prevent garbage collection if the object is no longer in use. It’s a nice but powerful tool for optimizing your code performance.
Hereโs a simple example:
let obj = { value: "Hello" };
let weakRef = new WeakRef(obj);
let derefValue = weakRef.deref();
console.log(derefValue.value); // "Hello"
obj = null; // Allow garbage collection
setTimeout(() => {
let derefValueAgain = weakRef.deref();
console.log(derefValueAgain);
// undefined if collected
}, 1000);
This JS function is perfect for caching scenarios or managing large datasets where you want to avoid memory leaks.
Why You Need These Functions?
These uncommon JS functions are not just a simple function, they solve real world problems with elegance and efficiency. By incorporating them into your projects, I promise that, you will be able to write cleaner code, reduce bugs, and impress your peers with your deep JavaScript knowledge.
How You Can Start Easily?
- Start small by testing Object.fromEntries() with your next data transformation task. Use our code examples mentioned above.
- Experiment with flatMap() on nested arrays to streamline your workflows.
- Use padStart() for formatting outputs, Promise.any() for faster API calls, and WeakRef.deref() for memory optimization in large applications.
Each function offers a unique way to explore and do it from your next project. Promise?
Conclusion
Diving into these five lesser-known JavaScript functions opens up new possibilities for your development work. They are not the flashiest tools, but their practicality and uniqueness can make you a confident coder indeed.
Whether you are building a personal project or a commercial app, these functions will boost your efficiency and give your project a hike.
๐๏ธโโ๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐ฅ Asp.net C# Developer
๐ Solution Architect
๐จโโ๏ธ Database Administrator
๐ข Speaker
๐ MCTS since 2009
Leave a Reply