1

var y = 1;
if(function f(){}){
  y += typeof f;
}

console.log(y);

Can anyone give an answer with an explanation about the above code?

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    What would be "appropriate behavior"? What exactly needs explanation? – Jonas Wilms Dec 23 '20 at 10:42
  • Exact duplicate of [Is a function hoisted if it is defined within an if condition?](https://stackoverflow.com/questions/31664040/is-a-function-hoisted-if-it-is-defined-within-an-if-condition) – Bergi Dec 23 '20 at 11:02

2 Answers2

2

Yes this appropriate behavior. The expression function f(){} will be converted to boolean. And will return true because function is always truthy.

console.log(Boolean(function f(){}))

Now lets come to the second part. Why y is 1undefined. The function f is a named function which is only visible in its own function body. You are try to access f in the body of if so its not accessible there hence it its type is "undefined"

console.log(typeof x); //undefined

So 1 + "undefined" is equal to "1undfined"(1 will converted to string and added to "undefined")

console.log(1+"undefined")
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • 1
    Just a small nitpick: `f` is `undefined` because the name of a named function expression is visible inside the function only. – Teemu Dec 23 '20 at 10:51
  • @Teemu _name of a named function expression is visible inside the function only_. I think its not correct. If we declare a named function in global scope its available in all the other function. Correct me if I am wrong – Maheer Ali Dec 23 '20 at 10:56
  • 1
    @MaheerAli Teemu said "named function *expression*", you're talking about global *declarations*. See https://stackoverflow.com/q/16288046/1048572 or https://stackoverflow.com/q/42456251/1048572 – Bergi Dec 23 '20 at 10:58
  • @Bergi So here the `function x(){}` is named function expession? – Maheer Ali Dec 23 '20 at 11:00
  • @MaheerAli _"So here the function x(){} is named function expession?"_ - yes it is. Try using `f` after the named function expression inside the `if` statement. You will find that `f` will be undefined. – Yousaf Dec 23 '20 at 11:02
  • @Yousaf Fine I got it thanks. – Maheer Ali Dec 23 '20 at 11:04
1

In your case, the function is declared and defined inside the if statement which is a block scope and its scope is only valid inside those parentheses. And the if block is getting evaluated because in JavaScript anything inside the if condition is coerced to an equivalent truthy or falsy value and for all truth values the if loop is executed.

var y = 1;
if (function f() {}) { // f has limited scope i.e within the if parenthesis only
  y += typeof f; //typeof f is "undefined" and 1+undefined gets concatinated as "1undefined"
}

console.log(y);

But if you do it like this you will get the desired result:

var y = 1;
let fun=function f(){} //Here the fun has scope outside if statement
if(fun){
  y += typeof fun; //type of fun will be function not undefined
}

console.log(y);
kapil pandey
  • 1,853
  • 1
  • 13
  • 26