Does the declaration contain a function in a conditional expression?

Hi, I am new to Javascript and I am reading Oreilly Javascript final guide. The declaration instruction section says:

enter image description here

I did a simple test:

var a = 1;
while(a < 5){
    a++;
    function double(a){return a * 2 };
    console.log(double(a));
}    

Node.js seems to not give me any error and works as expected. Any comments on this?

+5
source share
1 answer

This is not true. However, you should keep in mind that they may not behave as you might expect.

For example, this will work:

if( false) {
    function double(a) {return a*2;};
}
else {
    console.log(double(4));
}

, - , if, , "" ( <script> ) .

function if, , .

-1

All Articles