10 Not So Used JavaScript Functions: Unlocking Hidden Potential

Vikesh Mittal
3 min readJun 10, 2023

--

JavaScript, as a powerful programming language, offers a wide range of functions that developers utilize on a regular basis. However, there are several lesser-known functions tucked away in its documentation that can significantly enhance your coding skills and add elegance to your code. In this article, we will explore 10 rarely used JavaScript functions along with practical examples, showcasing their hidden potential and how they can enhance your development process.

String.prototype.trimStart() and String.prototype.trimEnd()

These functions remove whitespace from the beginning or end of a string, respectively. Here’s an example:

const text = "   Hello, World!   ";
const trimmedText = text.trimStart();
console.log(trimmedText); // Output: "Hello, World! "

Array.from()

This function creates a new array from an iterable or array-like object. Here’s an example:

const str = "Hello";
const charArray = Array.from(str);
console.log(charArray); // Output: ["H", "e", "l", "l", "o"]

Object.entries()

This function returns an array of key-value pairs from a given object. Here’s an example:

const user = {
name: "John Doe",
age: 30,
occupation: "Developer"
};

const entries = Object.entries(user);
console.log(entries);
// Output: [["name", "John Doe"], ["age", 30], ["occupation", "Developer"]]

Array.prototype.includes()

The includes() function checks if an array contains a specific element and returns a Boolean value. Here's an example:

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3)); // Output: true
console.log(numbers.includes(6)); // Output: false

Array.prototype.flatMap()

This function combines mapping and flattening an array into a single step. Here’s an example:

const numbers = [1, 2, 3, 4];
const doubledNumbers = numbers.flatMap(num => [num, num * 2]);
console.log(doubledNumbers); // Output: [1, 2, 2, 4, 3, 6, 4, 8]

Promise.allSettled()

Unlike Promise.all(), Promise.allSettled() waits for all promises to settle, regardless of their resolution status. Here's an example:

const promise1 = Promise.resolve("Resolved");
const promise2 = Promise.reject("Rejected");

Promise.allSettled([promise1, promise2])
.then(results => console.log(results));
// Output: [{ status: "fulfilled", value: "Resolved" }, { status: "rejected", reason: "Rejected" }]

Intl.DateTimeFormat()

This function provides localized formatting of dates and times. Here’s an example:

const date = new Date();
const formattedDate = new Intl.DateTimeFormat("en-US").format(date);
console.log(formattedDate); // Output: "6/10/2023"

Math.clz32()

This function returns the number of leading zero bits in the 32-bit binary representation of an integer. Here’s an example:

console.log(Math.clz32(1)); // Output: 31
console.log(Math.clz32(2)); // Output: 30

Reflect.apply()

This function dynamically calls a target function with a specified this value and an array of arguments. Here's an example:

function greet(name) {
console.log(`Hello, ${name}!`);
}

Reflect.apply(greet, null, ["John"]);
// Output: "Hello, John!"

Set.prototype.values() and Set.prototype.keys()

These functions return an iterator object containing the values or keys of a Set, respectively. Here’s an example:

const fruits = new Set(["apple", "banana", "orange"]);

for (const fruit of fruits.values()) {
console.log(fruit);
}
// Output: "apple", "banana", "orange"

for (const key of fruits.keys()) {
console.log(key);
}
// Output: "apple", "banana", "orange"

By familiarising yourself with these rarely used JavaScript functions and their practical examples, you can unlock their hidden potential and enhance your coding experience. These functions offer concise and efficient solutions to various programming challenges. Incorporating them into your projects can make your code more elegant, expressive, and maintainable. Embrace the lesser-known functions and explore the vast possibilities they bring to your JavaScript development journey.

--

--

Vikesh Mittal
Vikesh Mittal

Written by Vikesh Mittal

User Interface Architect | Passionate about Micro-frontends | Angular | React | https://vikeshmittal.com/

No responses yet