JavaScript coding tips

JavaScript is immensely popular in web technologies. Writing efficient code in JavaScript is really important. It is always good to know some coding tips and tricks about JavaScript.

Over the year we have collected different coding techniques that we are going to share in this post.

Use ternary operator

Verbose

    const age = 20;
    let result;

    if (x > 18) {
        result = "adult";
    } else {
        result =  "not adult";
    }
    console.log(result);

Succinct

    const age = 20;
    const result = age > 18 ? "adult" : "not adult";
    console.log(result);

Use Short-circuit

Verbose

    if (param !== null || param !== undefined || param !== '') {
        let arg = param;
    }

Succinct

    const arg = param  || 'new';