Unveiling Hidden Gems: Lesser Known Features of JavaScript

JavaScript, the versatile programming language that powers the web, is like a treasure trove filled with valuable gems. While many developers are well-acquainted with its fundamental features, there exist a plethora of lesser-known gems that can enhance your coding experience and make your code more elegant and efficient. In this article, we’ll delve into some of these hidden features that might just become your new favorites.


1. Destructuring with Default Values


Destructuring is a well-known technique in JavaScript that allows you to extract values from arrays or objects into separate variables. However, what’s less commonly known is that you can also provide default values in case the extracted value is undefined.


const person = {
  age: 23,
};
const { name = "Martin", age = 18 } = person;

In the above example, if the person object does not have a name property, the name variable will be assigned the default value ‘Martin’.


2. The Object.is() Method


Comparing values for equality in JavaScript can be tricky due to type coercion. The Object.is() method provides a straightforward way to compare values without any implicit type conversion.


console.log(Object.is(5, "5")); // false
console.log(Object.is(NaN, NaN)); // true

The Object.is() method is particularly useful when you want to determine if two values are exactly the same, even when dealing with special cases like NaN.


3. The ?. (Optional Chaining) Operator


Dealing with nested object properties can result in verbose and error-prone code. The optional chaining operator ?. simplifies this by allowing you to access nested properties without worrying about the existence of intermediate properties.


const cityPopulation = country?.city?.population || "Unknown";

In this example, if either country or country.city is null or undefined, the expression evaluates to ‘Unknown’, preventing errors.


4. The Promise.allSettled() Method


While many developers are familiar with Promise.all() for handling multiple promises, Promise.allSettled() is a hidden gem that can be incredibly useful. It returns an array of promise settlement results, regardless of whether they were fulfilled or rejected.


const promises = [promise1, promise2, promise3];
Promise.allSettled(promises).then((results) => {
  results.forEach((result) => {
    if (result.status === "fulfilled") {
      console.log("Fulfilled:", result.value);
    } else {
      console.log("Rejected:", result.reason);
    }
  });
});

This method is great for scenarios where you want to perform actions based on the outcomes of multiple promises, even if some of them fail.


5. The Intl Object for Internationalization


The Intl object provides built-in functionality for internationalization and localization tasks, often overlooked by developers. It offers capabilities like date and time formatting, number formatting, and collation (string comparison respecting language-specific rules).


const dateFormatter = new Intl.DateTimeFormat("en-US", {
  year: "numeric",
  month: "long",
  day: "numeric",
});
console.log(dateFormatter.format(new Date())); // August 22, 2023

By using the Intl object, you can create user-friendly applications that cater to diverse global audiences.


6. The String.raw Method


When dealing with strings containing escape sequences (e.g., \n, \t), the String.raw method can be a lifesaver. It returns the raw string without interpreting escape sequences.


console.log(String.raw`Hello\nWorld`);
// Output: "Hello\nWorld"

This can be particularly useful when working with regular expressions or creating templates that involve backslashes.


Unearth JavaScript’s Hidden Treasures


JavaScript is a language that constantly surprises developers with its hidden features and functionalities. While the basics are important, exploring these lesser-known gems can elevate your coding skills and improve the quality of your applications. From destructuring with default values to the powerful Promise.allSettled() method, these features can make your code more concise, robust, and expressive. So, embark on the journey of uncovering JavaScript’s hidden treasures, and let these gems shine in your codebase.