This is a collection of snippets which are good to keep in mind and become handy. Some points could be a part of a development guideline as well .

Process Events subscriptions

Detecting unhandled Promise rejections

The unhandledRejection event is triggered when a promise failure isn’t handled in your application. You can subscribe to this event to detect and handle such globally.

process.on('unhandledRejection', (error) => {
    console.error(error); // Output: Whoops, an Error occurred
});

globalThis

Instead of using global good approach is to use globalThis. This increases the code portability to multiple environments. JavaScript is used widely on server and client side as well. The globalThis is a standardization of the access to the global object using this reference. In the browser it is called window, or in Web Workers this is called self.

Naming Conventions

Let’s clarify first the terminology about the cases.

  • CamelCase / camelCase: words are not separated with special characters. All subsequent words after the first starts with a capital letter. According to the case of the first letter we differentiate between
    • UpperCamelCase alias Pascal case
    • lowerCamelCase

File names

Historical practice is lower case and to separate words by hyphens in file names. But the UpperCamelCase notation is now more common, so You should be not punished for that . It can be a personal favour by devs, but the key is that be consistent within one project.

  • Historical naming: dark-chocolate.js
  • More common new-age naming: DarkChocolate.js

Class Names

Use UpperCamelCase, like class AnnotationHelper { … }.

If You wrap that single class in a module the file name could be AnnotationHelper.js as explained above.

Variables, Functions, Methods, Objects, Arguments

Use lowerCamelCase. Starting JS as UI5 developer I used always Hungarian notation for variable names, but in Node.js using modern development environments or TypeScript this is not anymore the first priority due the context is more clear. I feel it personally now rather more disturbing in Node.js than helpful.

const two = 2;
const twentyFour = 24;

function keepKoalas(freshEukaContainers) {
}

Share this content: