The JS Bifrost - Incredible JavaScript Features

7 JavaScript Features You Should Start Using In Your Project

Sumit Naik
Globant
6 min readSep 11, 2020

--

Welcome to The JS Bifrost, your pathway to a rock-solid foundation for God-level JavaScript. This is the next article in the series, which highlights certain JavaScript features and tricks to write efficient code.

Asgard, the city of Thor, where bifrost exists

Javascript is a weird and funny language, yet so beautiful and powerful. Javascript developers are always striving to learn something new and even if we have used it for years, we keep discovering new features & tricks every other time. This article is an attempt to unfold and revisit such features and tricks that are sparingly used but are really handy and useful.
Let’s dive in.

1. Getters and Setters

Getters and Setters allow us to have more control over object properties by controlling how to set & access them. Sometimes, we may want to perform an operation before/after the value has been updated or accessed. Getters and Setters provide a perfect way to do this. A Setter is automatically invoked on assignment and Getter is invoked when a property is accessed. get and set are the keywords for defining getters and setters respectively.
Let’s see how they work:

What’s going on here?
We defined getters and setters for a property age in user object. As you can see, the property age does not directly exist in the object, rather we use another property _age to store & retrieve the actual value. Thus, age becomes a pseudo-property. Now, when we assign a value to user.age, the setter function is automatically invoked. It checks the conditions and assigns the value to _age property. Whenever we try to access user.age, the getter function invokes and returns a processed value using _age.

Okay, but how to define getters and setters on an already defined object?
With Object.defineProperty. Let’s see how that works:

We can define Getters and Setters in classes as well, in the same way as defined above.

2. Unary + & - Operators

A unary operation is an operation with only one operand. Unary + & - operators attempt to convert the operand to number type, the only difference being the unary - makes the number negative.
Many a times, we encounter a scenario where we need to convert a string to a number. These operators come to the rescue by just prefixing them to the given string. Let’s see how they work:

3. Exponentiation ( ** ) Operator

This operator is useful to quickly find the power of a number raised to a certain number. If you want to find x to the power y, simply use x ** y.

As an added advantage, it also accepts BigInts as operands.

4. in Operator

in operator can be used to quickly check if a specified property is present in an object.
Syntax: prop in object

Keep in mind that the in operator returns true even if the property is present in the prototype chain.

5. new.target property

This property is useful to detect whether a function or constructor was called using the new operator. It gives us a reference to that class or constructor function. The new.target pseudo-property is available in all the functions. However, in normal function calls, new.target is undefined.

You may think, why do we need to check if an instance was created with new operator? Consider the following example:

As we saw above, the global variable colour got updated unknowingly as ‘this’ is pointing to a window inside the function, which is called directly. To avoid such mishaps, we can rely on new.target property.

6. String Replace can accept a callback function

Many a times, we use the replace method on strings with regex/string as the first parameter and string to be replaced as the second.

const newStr = str.replace(regexp|substr, replacement);

It replaces all the matches with the provided replacement string. However, we can have more control over the replacement by providing a callback function as a second parameter. Consider an example:

This is a simple replacement, where we are replacing each number with ‘ # ’. What if we wanted to replace only the numbers > 20? We cannot simply do that with regex alone, right? We can achieve that with a callback function.

Notice how we used the unary + operator to convert a string to a number 🙂.

7. Formatting JSON

While dealing with APIs, we need to stringify objects - a very common scenario for a web developer. We simply use JSON.stringify() to achieve that. You know how ugly it is to visualize such JSON strings 😅. Look at the one below:

And it keeps getting uglier when we have larger & deeply nested objects. However, JSON.stringify() method also allows us to indent such strings, for better readability.
It accepts two optional parameters.

  • Replacer function - Allows us to filter the properties of an object to be stringified.
  • Space - A number or a string used to insert white space into the output JSON string. With a number, you can specify the number of spaces you want and with string, space characters like ‘\t’ (tab).

Conclusion

I know there are many other features and tricks apart from these, which we might not be aware of. If you know any such interesting use cases, please share those in the comments section.

If you enjoyed reading this article, please share :) Thank you!

Keep Coding JavaScript ❤️

Watch out our ‘The JS Bifrost’ series for more such amazing articles.

--

--