JavaScript

JavaScript

Made by DeepSource

Invalid variable usage JS-0043

Anti-pattern
Critical

Variables should be used inside of their binding context. This helps avoid difficult bugs with variable hoisting. It is a bad practice to use var declarations because variables declared using var can be accessed in a function-wide scope. They can even be accessed before declaration. In such cases, their value would be undefined because only declarations and not initializations are hoisted.

Bad Practice

function doIf() {
    if (cond()) {
        var build = true;
    }
    console.log(build);
}

function doIfElse() {
    if (cond()) {
        var build = true;
    } else {
        var build = false;
    }
    console.log(build)
}

Recommended

function doIf() {
    let build;
    if (cond()) {
        build = true;
    }
    console.log(build);
}