-3

var d = 1
if (function f() {}) {
  d += typeof f
}
console.log(d)

Output: 1Undefined

Why typeOf function is Undefined can someone explain??

I know we won't be using these typeof declarations in real time. But, I am trying to understand how Javascript handles function defintions...

  • Why in the world would you put function declaration inside IF? Worst case ever – Justinas Jul 11 '18 at 08:10
  • A `condition` goes between the `if`, not a declaration – Nick Parsons Jul 11 '18 at 08:13
  • 1
    Why all the downvotes? I think this is an interesting question. Why do function declarations work inside the conditional expression, and what are the rules for the scoping of them? It's not practical, but it's interesting behaviour. – caffeinated.tech Jul 11 '18 at 08:26

5 Answers5

1

The statement if (function f() {}) doesn't actually cause the function to be defined. Since ifs take expressions as input, the condition is essentially being evaluated as function expression.

Functions are truthy values, so if branch is executed, and your code is internally treated like this by JavaScript interpreter:

var d = 1;
d += typeof f;
console.log(d);

Obviously, f isn't defined at the second statement, hence the result is printed as 1undefined.

31piy
  • 23,323
  • 6
  • 47
  • 67
1

The if conditions are expressions, not statements. In your code, function f() {} is a function expression, not a function declaration, its name f can only be used in its body.

If the above is hard to understand for you, here is informal code which are equivalent to your code:

var d = 1;
tmp = function f() {};
if (tmp) {
    delete tmp;
    d += typeof f;
}
console.log(d);
terry.qiao
  • 1,959
  • 2
  • 8
  • 10
  • The `delete tmp;` is unnecessary. In a named function expression (`var foo = function bar(){}`), the function is only accessible as `foo` in the parent scope. `bar` would be undefined (except for inside the function body) – caffeinated.tech Jul 11 '18 at 08:50
  • @Caffeinated.tech `delete tmp;` is just to prevent `tmp` from being accessed by the subsequent codes. – terry.qiao Jul 11 '18 at 13:15
0

Only variable declarations are considered / accessed, so you can do:

var d = 1
if (g = function f() {}) {
  console.log(typeof g) // 'function'
  console.log(typeof f) // 'undefined'
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
-1

you should create function before if and call it in if

like that:

var d = 1
function func() {
 return true;
}
if (func) {
 // do some thing
}
console.log(d)
Abdi
  • 589
  • 5
  • 16
  • 3
    That's not an answer to the question, the question is why the type of that function is undefined, not how to make the code "work" – A. Llorente Jul 11 '18 at 08:17
-1

typeof will return undefined because your function f has not been programmed to return anything. in other words, you are trying to find the type of something that has not been defined.

e.g.

var d = 1;
    
function f() {
    return 2;
}

if (f) {
  d += typeof f
}
console.log(d)

The output is 1function because you are adding a number and a string -typeof returns a string- so javascript concatenates the number and the string.

If you'd like to know why function declarations dont work inside if statements, its been answered before here

MingLi
  • 53
  • 1
  • 9
  • Also, because your `function f` has not been programmed to do anything, it will give you an issue in the **if statement** because the if statement checks to see if something is true or false. In your case its neither because your `function f` is undefined. – MingLi Jul 11 '18 at 08:46
  • 1
    `typeof` doesn't care if the function returns anything (try `typeof function(){}`). Nor does the if statement as the function isn't being executed – caffeinated.tech Jul 11 '18 at 08:50