var add = function addnums(a,b)
{
return a+b;
}
alert("sum is " + addnums(10,20));
why can't i use addnums
directly in the above Javascript ?
PS : I know other alternatives, the question is why the above specified method doesn't work.
var add = function addnums(a,b)
{
return a+b;
}
alert("sum is " + addnums(10,20));
why can't i use addnums
directly in the above Javascript ?
PS : I know other alternatives, the question is why the above specified method doesn't work.
You can.
var add = function addnums(a,b)
{
return a+b;
}
alert("sum is " + (function(a,b) { return a+b; })(10,20) );
Plus you may shorten the syntax:
var addnums = function(a,b) { ... }
or
function addnums(a,b) { ... }
You have an error writing the function write like the following:
var addnums = function(a,b)
{
return a+b;
}
alert("sum is " + addnums(10,20));
:)
var add; is a declaration. Here add is declared and assigned a function you need not name the function after keyword function, variable name will take the function name when you define a function in this way. I think you understand now.