0

The example below must output for sure function, as the value of x is "1" (integer) and then we are passing a parameter named "f" (of a function). It doesn't matter whether this function does something or is blank, but I am sure -- that variable f --> points to function. so typeof(f) will surely return function.

Now, adding of an integer and "function" (as typeof always returns a string) is going to be a string --> 1function.

Now, amazingly the output is "1undefined". How?

<script>

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

</script>

As per answer by Ellepsis that declarations don't take inside if() braces and only boolean is returned. Explain this then, why is it returning 3?

<script>

var x = 1;
if (y = 2) {
x = x + y; 
}
console.log(x);

</script>
Deadpool
  • 7,811
  • 9
  • 44
  • 88

3 Answers3

3
if (function f(){}) {

A function declaration will create a variable with the same name in the current scope.

A function expression will only create a variable with the same name inside itself.

So the variable f only exists:

if (function f(){    })
                 ^^^^
                 here

The function expression itself evaluates as a function, which the if statement picks up as a truthy value but there is no f variable in scope.


Explain this then, why is it returning 3?

You are explicitly assigning a value to a variable. That variable remains in scope.

You would get the same effect if you did this:

if (f = function f(){}) {

So:

(function f(){})
  • Creates a function
  • Names the function f
  • Creates a variable inside the function called f containing a reference to a function
  • Evaluates as a function, which is a true value, which is tested by the if

While:

(f = function f(){})
  • Creates a function
  • Names the function f
  • Creates a variable inside the function called f containing a reference to a function
  • Creates a variable f outside the function
  • function f(){} evaluates as a function and is assigned to f by the =
  • f = function f(){} also evaluates as a function, which is a true value, which is tested by the if
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • So, the case comes different if we pass a variable referring to string, boolean, number, array, or undefined? like `y=3` or `z="peter"`? Then y & z exist in real world but not if we pass function `f`(){} --- I assume with same rules we already gave a name `f` to a function. Haven't we? – Deadpool Feb 26 '19 at 09:50
  • @Deadpool — Functions are not variables. – Quentin Feb 26 '19 at 09:52
  • ok. Means assignment is power powerful and would logically make a variable exist than writing function `func(){}` --- meaning func never exists? I thought this is another way of creating a variable, say `func` which will point to memory location containing function data. isn't it? – Deadpool Feb 26 '19 at 09:53
  • "meaning func never exists" — The function exists. – Quentin Feb 26 '19 at 09:53
  • "I thought this is another way of creating a variable, say func which will point to memory location containing function data." — JavaScript isn't assembly you don't deal directly with memory locations in it. I explained the difference between a function expression and a function declaration in the first section of this answer. – Quentin Feb 26 '19 at 09:54
  • yah, I named my function func above! `function func(){}` – Deadpool Feb 26 '19 at 09:55
  • 1
    Why is `f` not hoisted here? Something like [this](https://stackoverflow.com/questions/35250601/hoisting-of-functions-inside-conditionals) – adiga Feb 26 '19 at 09:59
  • 1
    Function expressions are not hoisted (and since I already said they don't create a variable `f` in the outer scope … what would hoisting do anyway?!) – Quentin Feb 26 '19 at 09:59
  • Got you. So, `if (function f(){})` is a function expression and not function declaration. – adiga Feb 26 '19 at 10:08
1

You are getting undefined because function f is not defined in the code anywhere. Writing the function inside an if statement does not define the function. The code just assumes it as a truthy value, a condition for the if statement but in real f does not exists. You can define f outside and it will work fine or you can just perform the assignment in the if and then also it will work

var x = 1;
if (f=function(){}) {
  x += typeof f;
}
console.log(x);
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • please see updated question. Your logic doesn't seem fit for all cases ... strangely! – Deadpool Feb 26 '19 at 09:46
  • In your second snippet you are assigning a value and then using it which is working. See the edits. When you assign a function it will work too. If you just define y in the if statement and not assign any value it will give error too. That is the difference between your two snippets – ellipsis Feb 26 '19 at 09:53
-2

var x = 1;
var f;
if ( f = function() {} ) {
    x += typeof f;
}
console.log(x);
  • 1
    I know that. But, just asking why in the given example the output is such; Not how can we bring in required result (that we want). – Deadpool Feb 26 '19 at 09:45