Change this:
sum(2,2);
var test = sum();
To this:
var test = sum(2,2);
The first code isn't technically wrong it just isn't doing what you're trying to do. You're calling the sum function with the appropriate values but never setting it's return value to any variable so it just gets thrown away. You seem to be under the impression that the value will "stick" to the function and this isn't the case. (Some BASIC languages can make it seem this way though. Perhaps that's where your misconception is coming from.)
Your second call is essentially the equivalent of
var test = sum(null, null);
and when you concatenate two null values you get null again.
sum(2,2)executes the function.var test = sum()assigns the result ofsumwithout parameters, of courseNaN.sum(2,2)does return4,sum()alone does returnNaN- what did you expect (or: which tutorials did you read)?var test = sum()would not run the function. Instead it would just retrieve the returned value. But then again, I have been studying javascript (and programming in general) mere few weeks.