jeudi 21 septembre 2017

Confusion with block statements

Noob Question

I was going through the ES6 features website and I found this piece of code

function f (x, y, z) {
    if (y === undefined)
        y = 7;
    if (z === undefined)
        z = 42;
    return x + y + z;
};
alert(f(1))

When I don't pass the y ans z params to the function,in line 4 if (z === undefined) is true. What I don't understand is, how does the compiler know that only line 5 is part of the if block and not return statements.

In other words, how does the compiler know to execute it in this way

if (z === undefined) {
    z = 42;
}
return x + y + z;

and not in this way

if (z === undefined) {
    z = 42;
    return x + y + z;
}

How does the compiler know that the return statement is not a part of the second if statement?

Aucun commentaire:

Enregistrer un commentaire