JavaScript Best Practices

We've covered several introductory concepts for JavaScript programming in this course and how to debug your code. We'll review some coding guidelines and best practices you'll use as a JavaScript developer to ensure consistency, readability, security, and accessibility. Read this article to learn about some of the best practices for developing programs.

Operators and comparison


Ternary operators

Ternary operators should be put on a single line:

let status = (age >= 18) ? 'adult' : 'minor';

Not nested:

let status = (age >= 18)
  ? 'adult'
  : 'minor';

This is much harder to read.


Use strict equality

Always use strict equality and inequality.

Do this:

name === 'Chris';
age !== 25;

Not this:

name == 'Chris';
age != 25;


Use shortcuts for boolean tests

Use shortcuts for boolean tests – use x and !x, not x === true and x === false.