DEV Community

Rova Ranaivo
Rova Ranaivo

Posted on • Updated on

JavaScript: Discovering Hidden Gems For Advanced Developers

As a vibrant language used by millions of developers, JavaScript is continually evolving and improving. Though you may be well-acquainted with its common syntax and usage, let's venture off the beaten path and explore some lesser-known aspects of JavaScript. These might help you write more expressive, elegant, and efficient code.

1. Proxies

The Proxy object in JavaScript is a powerful feature, which is not as frequently used as it should be. Proxies allow you to create a placeholder for another object, which can intercept and redefine fundamental operations for that object.

Here's an example of a proxy being used to intercept a property access operation

let handler = {
    get: function(target, name) {
        return name in target ? target[name] : 42;
    }
};

let p = new Proxy({}, handler);
p.a = 1;

console.log(p.a, p.b); // 1, 42
Enter fullscreen mode Exit fullscreen mode

In the above code, p is a proxy for an empty object. The handler object defines a get method which provides a custom behavior whenever a property is accessed on the proxy object.

2. Tagged Template Literals

While many developers use template literals for string interpolation, not everyone knows they can be 'tagged'. This means applying a function to the template literal, which allows for string parsing customization.

Here's an example

function tagged(strings, ...values) {
    return strings.reduce((result, string, i) => 
        `${result}${string}${values[i] || ''}`, '');
}

let adjective = "better";
console.log(tagged`You will be a ${adjective} JS developer after this article.`); 
// "You will be a better JS developer after this article."
Enter fullscreen mode Exit fullscreen mode

The tagged function gets the literal strings as its first argument and the substitutions as its remaining arguments.

3. Optional Chaining Operator

Optional chaining (.) allows you to read the value of a property located deep within a chain of connected objects without having to check each property in the chain.

let book = {
  title: "The Art of JavaScript",
  author {
    firstName: "John",
    lastName: "Doe"
  }
};

console.log(book.author.firstName); // John
Enter fullscreen mode Exit fullscreen mode

In this example, JavaScript will evaluate the expression up to the point where the first undefined or null value is located, making code more readable and less prone to runtime errors.

4. Using in Operator

The in operator checks whether an object has a specific property. It can also check if an index exists in an array. Here's a simple demonstration

let car = {make: Tesla, model: Model 3};
console.log("make" in car); // returns true

let arr = [10, 20, 30];
console.log(2 in arr); // returns true
Enter fullscreen mode Exit fullscreen mode

This operator is often overlooked but can be a handy tool in your JavaScript toolkit.

5. Using Async Generators and Iterators

With the advent of AsyncAwait, asynchronous code in JavaScript has become more straightforward to write and understand. This is further simplified by Async Generators and Iterators, although they're not often used by many developers.

Here's a simple example of an async generator function

async function asyncGenerator() {
    yield Hello;
    yield World;
}

let generator = asyncGenerator();
console.log(await generator.next()); // { value 'Hello', done false }
console.log(await generator.next()); // { value 'World', done false }
Enter fullscreen mode Exit fullscreen mode

The function asyncGenerator is an asynchronous generator function that yields two values.

These hidden gems are just a tip of the iceberg. JavaScript is a language with a lot of depth, and even advanced developers might not be familiar with all of its nuances. However, the exploration of its lesser-known features can lead to cleaner and more efficient code, enhancing your journey as a JavaScript developer.

I hope that this article has offered some inspiration to dive deeper into JavaScript and explore the treasures it holds. Happy coding!

Tell me what you think about it!

Top comments (0)