This article aims to show some tips and tricks that can be used in your code to make the it cleaner or more readable.

Required function parameters

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. We can take the advantages of this ES6 feature and make a function which throws an error and assign it as default value for required parameters.

Another way to deal with required function parameters is to create a global property named required . It can be assigned as default function parameter, so if no value has been specified in the function call, the getter handler will be triggered and the error will be thrown.

Effective Data Validation

At some point we need to validate our data inside methods. My favorite approach is to use assert like functions. Assert functions are a short way of instructing the program to check the condition and if the condition is not True, it will throw error.

Replacing texts in a string

The String prototype method named replace includes some interesting replacement patterns:

$& Inserts the matched substring.

Inserts the matched substring. $` Inserts the portion of the string that precedes the matched substring.

Inserts the portion of the string that precedes the matched substring. $' Inserts the portion of the string that follows the matched substring.

Improve the code readability with async / await syntax

Async/await is a new and an elegant way to write asynchronous code in Javascript. Usually, I’m using it with fetch .

A more concise way to write this function can be:

Check the code performance with performance.now()

If you have a portion of code that needs to run really fast, it can be inspected with performance.now() function. Unlike regular timestamps created with Date.now() the method performance.now() generates high resolution timestamp (DOMHighResTimeStamp) .

Conditional logging without if / else

We can use console.assert(condition, msg) to log something when the condition is falsy. If the assertion is true, nothing happens.

Using setTimeout() with variable parameters

setTimeout allows us to run a function once after the interval of time. Instead of wrapping our method with an anonymous function, we can pass the function parameters directly:

( not quite sure if is more readable 😂 but good to know … )

Filter falsy values

If you need to filter falsy values like ( 0 , undefined , null , false , etc.) out of an array, you can use this trick:

Multiple conditions in an if statement

If you have to deal with checking a large number of conditions, you can use a clean looking alternative of if and || operator which scales nicely:

Or even better, using includes method:

RegExp named capture groups

ES2018 introduced named capture groups into JavaScript regexes. Named captured groups provide less confusion with regular expressions, it improves the readability and quality of the code, because you can find easily a group using an identifier.

That’s it 🤪! What are your favorite JavaScript tricks?