
3.2 Coding Style - Link

{ on the same line, after a space{ without a line breakif (conditon) {
// body
}
Why the term "Egyptian"?
The term "Egyptian" comes from the way the braces look visually, which some people feel resemble the shape of the ancient Egyptian pyramids.
The opening brace is at the end of the line (representing the base of a pyramid), and the closing brace is at the same indentation level (which visually resembles the peak of a pyramid).
No one likes to read a long horizontal line of code. It’s best practice to split them.
// backtick quotes ` allow to split the string into multiple lines
let str = `
ECMA International's TC39 is a group of JavaScript developers,
implementers, academics, and more, collaborating with the community
to maintain and evolve the definition of JavaScript.
`;
And, for if statements
if (
id === 123 &&
moonPhase === 'Waning Gibbous' &&
zodiacSign === 'Libra'
) {
letTheSorceryBegin();
}
Horizontal indents: 2 or 4 spaces
show(parameters,
aligned, // 5 spaces padding at the left
one,
after,
another
) {
// ...
}
Vertical indents
function pow(x, n) {
let result = 1;
// <--
for (let i = 0; i < n; i++) {
result *= x;
}
// <--
return result;
}
Try to avoid nesting code too many levels deep.
For example, in the loop, it’s sometimes a good idea to use the continue directive to avoid extra nesting.
For (let i = 0; i < 10; i++) {
if (!cond) continue;
...// no extra nesting level
}
A similar thing can be done with if/else and return.
// Option 1
function pow (x, n) {
if (n < 0) {
alert("Negative 'n' not supported");
} else {
let result = 1;
for (let i = 0; i < n; i++) {
result *= x;
}
return result
}
}
// Option 2
function pow (x, n) {
if (n < 0) {
alert("Negative 'n' not supported");
return;
}
let result = 1;
for (let i = 0; i < n; i++) {
result *= x;
}
return result;
}
n < 0 is handled early on. Once the check is done we can move on to the “main” code flow without the need for addtional nesting.There are 3 ways to organize the functions.
// function declaratons
function createElement() {
...
}
function setHandler(elem) {
...
}
function walkAround() {
...
}
// the code which uses them
let elem = createElement();
setHandler(elem);
walkAround();
// the code which uses the functions
let elem = createElement();
setHandler(elem);
walkAround();
// --- helper functions ---
function createElement() {
...
}
function setHandler(elem) {
...
}
function walkAround() {
...
}
Popular JavaScript Style Guide
Linters are tools that can automatically check the style of your code and make improving suggestions.
All of them can do the job. The author user ESLint.
Most linters are integrated with many poppular editors: just enable the plugin in the editor and configure the style.
For instance, for ESLint you should do the following:
Install Node.js
Install ESLint on project ( it’s manual start or can use quick start )
npm install eslint --save-dev
-–save-dev is used to install dependencies for development purposesCreate ESLint setting file
npx eslint --init
// eslint.config.mjs
import globals from "globals";
import pluginJs from "@eslint/js";
/** @type {import('eslint').Linter.Config[]} */
export default [
{
languageOptions: {
globals: globals.browser,
},
// set your rule
rules: {
indent: ["error", 2],
"no-console": "warn",
},
},
pluginJs.configs.recommended,
];
Install/enable the plugin for your editor that integrates with ESLint. The majority of edifors have one.