A professional developer will write the code thinking about your later self, for your 'co-workers' or just 'the other guy' and not just for the machine. If you worry about how the code it is written and not only if it work or not then you care about the clean code

A professional developer will write the code thinking about your later self, for your “co-workers” or just “the other guy” and not just for the machine. If you worry about how the code it is written and not only if it work or not then you care about the clean code, we can think of that as the code written in such a manner that is self-explanatory, easy to understand by humans and easy to change or extend. In general, you should not to repeat yourself, meaning you shouldn’t write duplicate code, and not to leave tails behind you such as unused functions and dead code, as soon you decide it’s no longer needed, delete it, later you might forget what it was used for.

“Even bad code can function. But if the code isn’t clean, it can bring a development organization to its knees” Robert C. Martin

Strong type checks

Use === If not handled properly, it can dramatically affect the program logic.

0 == false // true
0 === false // false

2 == "2" // true
2 === "2" // false

Variables

Name your variables thinking to reveal the intention behind it, don’t worry if you have long names, this way they become easier to understand for any person and the code more readable.

Bad:

const y = new Date().getFullYear();

let young;
if (user.age < 18) {
  young = true;
}

Good:

const MIN_AGE = 18;
const currentYear = new Date().getFullYear();

const isUserYoungerThanAllowed = user.age < MIN_AGE;

Don’t add extra words to the variable names if they are not need.

Bad:

let nameValue;

Good:

let name;

Don’t enforce the need for memorizing the variable context.

Bad:

const fruits = ['Apple', 'Orange', 'Banana'];
fruits.forEach(f => {
    doSomeStuff();
    doSomething();
    // ...
    // ...
    // ...
    // What is 'f' for?
    register(f);
});

Good:

const fruits = ['Apple', 'Orange', 'Banana'];
fruits.forEach(fruit => {
    doSomeStuff();
    doSomething();
    // ...
    // ...
    // ...
    register(fruit);
});

Don’t add unnecessary context.

Bad:

const fruit = {
    fruitName: 'Apple',
};

fruit.fruitName;

Good:

const fruit = {
    name: 'Apple',
};

fruit.name;

Conditionals

Use conditional shorthands. Use this approach only for boolean values and if you are sure that the value will not be undefined or null. Be explicit

Bad:

if (isValid === true) {
    // do something
}

if (isValid === false) {
    // do something
}

Good:

if (isValid) {
    // do something
}

if (!isValid) {
    // do something
}

Avoid negative conditionals.

Bad:

function isValidUser(user) {
    // do something
}

if (!isValidUser(user)) {
    // do something
}

Good:

function isInvalidUser(user) {
    // do something
}

if (isInvalidUser(user)) {
    // do something
}

Functions

Use descriptive names. Considering that it represents a certain action so a function name should be a verb fully exposing the intent behind it as well as the intent of the arguments. Your functions should do one thing only on one level of abstraction.

Bad:

function notify(user) {
    // do something
}

Good:

function notifyUser(emailAddress) {
    // do something
}

Use default arguments instead of conditionals.

Bad:

function createShape(type) {
    const shapeType = type || 'cube';
    // do something
}

Good:

function createShape(type = 'cube') {
    // do something
}

Think about single responsability a function should just do one thing. Avoid executing multiple actions within a single function.

Bad:

function notifyUsers(users) {
    users.forEach(user => {
        if (user.isVerified()) {
            notifyUser(user);
        }
    });
}

Good:

function notifyVerifiedUsers(users) {
    users.filter(isUserVerified).forEach(notifyUser);
}

function isUserVerified(user) {
    return user.isVerified();
}

Don’t use flags as parameters in functions they are telling you that the function is doing more than it should.

Bad:

function createFile(name, isPublic) {
    if (isPublic) {
        fs.create(`./public/${name}`);
    } else {
        fs.create(name);
    }
}

Good:

function createFile(name) {
    fs.create(name);
}

function createPublicFile(name) {
    createFile(`./public/${name}`);
}

Use Object.assign to set default objects.

Bad:

const shapeConfig = {
    type: "cube",
    width: 200,
    height: null,
};

function createShape(config) {
    config.type = config.type || "cube";
    config.width = config.width || 250;
    config.height = config.width || 250;
    
    // do something
}

createShape(shapeConfig);

Good:

const shapeConfig = {
    type: "cube",
    width: 200
};

function createShape(config) {
    config = Object.assign(
        {
            type: "cube",
            width: 250,
            height: 250
        },
        config
    );

    // do something
}

createShape(shapeConfig);