Introduce JavaScript modern practices
From this article, you will learn how WebStorm-provided coding assistance helps you follow key best practices in modern JavaScript for cleaner, more maintainable, and more performant code.
When dealing with old codebases, you may encounter outdated patterns and practices. WebStorm helps you by highlighting such patterns and suggesting quick fixes.
Declare variables with let and const
let and const provide block-scoping, which is more predictable and reduces unexpected errors that can happen when declaring variables with var, which is function-scoped.
Although var declarations may be used on purpose, in old code it can result from the old approach.
WebStorm detects usages of var and suggests replacing it with let or const.
Hover over a highlighted
vardeclaration and click the Convert to const link.
Place the caret at a
vardeclaration, press Alt+Enter, and select Convert to let or Convert to const from the popup list.
Example


Use Classes instead of Function: prototype
Although in many old codebases you may run into the function prototype approach for emulation of classes, it is recommended that you use classes because they have much cleaner syntax.
WebStorm suggests a refactoring that you can invoke with a Convert to class context action.
Place the caret at the name of the function to be converted and press Alt+Enter.
From the list, select Convert to class.
In the Refactoring Preview, review the suggested updates and click Refactor, when ready.
Use # for true privacy for class fields
In older JavaScript code, it was common to use an underscore (_) as a convention to denote private properties or methods in classes. However, this doesn’t actually enforce privacy – it just serves as a signal to developers that something is meant to be private.
When you really need private fields in classes, use the # syntax. This is an official language feature that enforces true privacy.
Using real private fields ensures that the data is truly encapsulated, preventing accidental or malicious access from outside the class. The underscore convention only provides a visual cue and can easily be misused, while # private fields guarantee privacy by design. This results in more robust and maintainable code.
WebStorm suggests the # syntax as you type as well as in intention actions and quick fixes.
Start typing a class constructor and press Tab to accept the suggestion with the
#syntax.
Hover over the unresolved reference and click Create field '#<field name>'.

Alternatively, place the caret at the unresolved reference, press Alt+Enter, and select Create field '#<field name>' from the list.

Use ?? nullish coalescing instead of || logical OR
The logical OR (||) operator can be used to assign default values when a variable is undefined or null. However, this can behave unexpectedly when the variable holds values like 0, false, or an empty string (""), because || treats them as falsy and substitutes the default value.
It is recommended that you use the nullish coalescing operator (??) instead of || when resolving default values. It only checks for null or undefined, leaving other falsy values (like 0, false, "") intact.
The ?? operator provides a more precise way of handling default values in cases where null or undefined should trigger the fallback. It prevents errors caused by using ||, which may unintentionally override valid falsy values. Using nullish coalescing results in more predictable behavior, improving both code clarity and reliability.
WebStorm suggests the ?? operator as you type.
Start typing an expression and press Tab to accept the suggestion with the
??operator.
Use arrow function expressions
Arrow functions provide a more concise syntax, automatically bind the this context, which can be particularly helpful in class methods where this can easily get lost.
With WebStorm, you can introduce an arrow function using a dedicated context action.
Place the caret inside an anonymous function and press Alt+Enter.
From the popup list, select Convert to arrow function.
Use optional chaining
The optional chaining operator (?.) automatically checks whether a property, an array element, or a method exists before attempting to access it. If any part of the chain is null or undefined, it returns undefined rather than throwing an error. Without optional chaining, this check requires verbose and repetitive code.
WebStorm suggests a context action to introduce a ?. operator.
Place the caret at an expression to be converted and press Alt+Enter.
From the popup list, select Use optional chaining.

Use async/await syntax
The async/await syntax simplifies working with asynchronous operations by removing the need for chaining .then() and .catch(). It makes your code more readable, more maintainable, and easier to follow, especially when dealing with multiple async calls.
With WebStorm, you introduce the async/await syntax into your code via the Convert to async function context action.
Place the caret at a function to introduce the
async/awaitsyntax to and press Alt+Enter.From the popup list, select Convert to sync function.

Interaction with object keys and values
In older JavaScript code, interacting with the keys and values of an object often involved manual looping with for...in or Object.keys(), followed by accessing values through bracket notation or dot notation. This can lead to verbose and less intuitive code.
It is recommended that you use modern methods such as Object.entries() or Object.values() for working with object keys and values. These methods simplify the process and return useful structures like arrays, making your code more concise and easier to work with. These methods reduce the amount of boilerplate needed for iterating over objects and improve code clarity, especially when dealing with complex or dynamic data structures. They also support easier transformations of objects into other forms (e.g. arrays), making data manipulation more flexible and efficient.
Although WebStorm does not currently provide a quick fix for converting for...in loops to Object.entries() or Object.values(), you can ask an AI agent to suggest code improvements.
Start an Air session by clicking New Session on the toolbar.
Specify the prompt, for example:
Suggest improvements for the code in <path to your file>Examine the suggested improvements.

Use strict equality (===)
Using strict equality (===) instead of loose equality (==) leads to more predictable and reliable behavior because strict equality does not perform type coercion but compares both value and type directly.


WebStorm detects potential problems that using the loose equality may cause, highlights unreliable code fragments, and suggests a quick fix.
Hover over a highlighted loose equality and click Replace with '==='.

Place the caret at a
===equation, press Alt+Enter, and select Replace with '===' from the popup list.