This question sparks curiosity, prompting a deeper dive into the world of javascript expressions. In essence, an assignment in javascript takes the form of
variable_name = expression
Upon creating the variable
, the expression
is processed and evaluated.
//Therefore,
number = 3 * 5
//becomes
number = 15
Functions
can be invoked with an expression, literal value (such as a string
or int
), or variable name
// '|' denotes 'or'
function(expression | literal | variable)
If an expression is passed to a function like function(expression)
, the expression is first evaluated before being passed as an argument to the function.
//For instance,
function(3*5)
//is equivalent to
function(15)
A similar concept applies to nested function calls. When one function is called inside another, it is evaluated first with the resulting value becoming the outer function's argument.
Consider this example:
function increment(number){
return number + 1
}
n = 1
document.write(increment(n))
The execution starts with document.write
being called with the parameter increment(n)
where n = 1
//Which leads to
increment(n) = increment(1) = 2
//As we progress,
document.write(increment(n))
//is essentially
document.write(2)
//!!
Hopefully, this brings clarity!
Edit:
To relate back to your scenario,
function multiNum(x,y){
return x*y
}
var num = multiNum(3,4) // num = 12
//Thus,
document.write(num)
//results in
document.write(12)