Skip to content
loading...

re: Vanilla JS, back to basics (Part 2: Hoisting) VIEW POST

FULL DISCUSSION
 

I advise you to always declare your functions in a variable [...]

Three lines above you gave the proper advise to avoid var and use let/const and now you immediately apply the anti-pattern. To avoid surprises, functions should be stored in const-declared variables so that we receive an error:

const hello = function() {
    const a = function() {
        return 3;
    }
    return a();
    const a = function() { //⇒ Uncaught SyntaxError: Identifier 'a' [...]
        return 4;
    }
}
console.log(hello());

or, if you want to overwrite the already defined function (why for God’s sake?), use let

const hello = function() {
    let a = function() {
        return 3;
    }
    return a();
    a = function() {
        return 4;
    }
}
console.log(hello()); //⇒ 3
 

Thanks a lot Aleksei for your remarks. I totally agree with you. I will try to rearrange or reorder these explanations

code of conduct - report abuse