Skip to content

Latest commit

 

History

History
127 lines (88 loc) · 2.81 KB

File metadata and controls

127 lines (88 loc) · 2.81 KB

JavaScript Basics

1. Introduction

CodeAcademy Tutorial

You can also watch a popular JavaScript basics crash course video on Youtube.

2. Modules

Read this article

Video - https://www.youtube.com/watch?v=hyYbs3SANRo

3. Best Practices: Indentation

Ensure that your code indentation is good. Submitting code that looks like the example given is unprofessional and makes it difficult to read for you as well as anyone who has to work with your code.

Bad indentation:

 function problem1(inventory,passid){

if(Array.isArray(inventory)){

return inventory;}

else{
    return [];
}
}

module.exports=problem1;

Good indentation:

function problem1(inventory, passid) {
    if(Array.isArray(inventory) && passid<50) {
        return inventory;
    } else {
        return [];
    }
}

module.exports = problem1;

An easy way to indent your code is to press Ctrl + Shift + I in VSCode.

If you are on a Mac, try Cmd + Shift + I

4. Best Practices: Variable Naming

Ensure that your variable names are descriptive. Variables names looks like the examples below are unprofessional and makes it difficult to read for you as well as anyone who has to work with your code.

Bad Naming:

const a = [1, 2, 3, 4, 5];
const a1 = [1, 4, 9, 16, 25];

Good naming:

const numbers = [1, 2, 3, 4, 5];
const squares = [1, 4, 9, 16, 25];

This also applies to functions

Bad Naming:

const numbers = [1, 2, 3, 4, 5];

function calc(n) {
    return n * n;
}

const squares = numbers.map(calc);

Good naming:

const numbers = [1, 2, 3, 4, 5];

function squareNumber(number) {
    return number * number;
}

const squares = numbers.map(squareNumber);

Naming is tricky for every engineer. But that does not mean you don't do it. It means you think about what to name it while you are writing code, not after you have finished.

4. Best Practices: if else

Keeping your code as readable as possible will ensure that you don't run into issues later on. One way to ensure this with if else if is to always use {} with the statements.

Bad Usage:

let sum = 10;

if(value %2 == 0)
    sum += value;
else if(value == 0)
    sum += 1;
else
    sum += -1;

Good usage:

let sum = 10;

if(value %2 == 0) {
    sum += value;
} else if(value == 0) {
    sum += 1;
} else {
    sum += -1;
}

This ensures that there is never any confusion about which code runs and when.

It also ensures that there will be no chance of code not getting executed because it was placed on an adjacent line and is missed by the interpreter.