javascript

How to write more readable JavaScript conditionals

How to write more readable JavaScript conditionals

  • October 4, 2022

One of the practices of clean code is to make it more readable. And a fairly common use case in code bases is conditionals. I recently came across a way of writing more readable JavaScript conditionals and thought of sharing it with everyone. Consider the following conditional: The mind takes...

Deep copying in JavaScript using structuredClone

Deep copying in JavaScript using structuredClone

  • August 23, 2022

For as long as anyone can remember, deep copying in JavaScript was not a built-in feature and we had to resort to libraries or workarounds to create a deep copy of a JavaScript value. That has changed now. The platform now offers a built-in function that does deep copying for...

How to prevent npm install for unsupported Node.js versions

How to prevent npm install for unsupported Node.js versions

  • March 17, 2022

npm configurations allow us to do quite a lot of nifty things. One of them is to allow the project to set the Node.js version that needs to be used in order to run the project. This also provides us with the functionality to prevent npm install for unsupported Node.js...

Detecting dark mode preference using JavaScript

Detecting dark mode preference using JavaScript

  • March 2, 2022

As dark themes have become popular across the web and across operating systems, we might want to check the user’s operating system preference when they visit our website. This way, we can initialize our website accordingly after detecting their dark mode preference. We can make use of the prefers-color-scheme media...

JavaScript: Split string and keep the separators

JavaScript: Split string and keep the separators

  • February 1, 2022

String.prototype.split() is a valuable method to split strings based on a delimiter. There often comes a scenario when we want to split a string and keep the separators in the result. The same JavaScript method provides a way to do so. Before we get into that, for people who are...

Printing JavaScript stack traces using console.trace

Printing JavaScript stack traces using console.trace

  • January 26, 2022

The console object in JavaScript has a lot more useful functions than the most frequently used console.log method. Debugging errors and finding execution flow can be a lot easier by making use of the console.trace method. It allows us to print the current stack trace where the method was called....

Overriding nested dependencies in NPM

Overriding nested dependencies in NPM

  • January 18, 2022

Whenever we install a particular package, it is common to come across a problem with a dependency’s dependency. With the release of npm 8.3, an overrides attribute has been added to solve this problem and allow overriding of nested dependencies in NPM. The problem Let us say your project depends...

Automatic batching in React 18 helps avoid re-rendering

Automatic batching in React 18 helps avoid re-rendering

  • January 13, 2022

Remember the earlier versions of React that used to batch multiple state updates inside event handlers such as click or change to avoid multiple re-renders? React 18 has added automatic batching for all use cases to improve that performance optimization even further. With React 18, state updates in event handlers,...