Why does the following javascript code run the way it does?
var plusOne = function(num){
return num+1;
};
var total=plusOne(1);
console.log(total);
var total2=plusOne(3);
console.log(total2);
If I am correct
var total=plusOne(1);
console.log(total);
returns a value of 2 to the variable total AND to the variable plusOne which then logs to the console "2", but if the value of plusOne is now 2 than why does
var total2=plusOne(3);
console.log(total2);
return as a value of 4 because isn't the actual code that it is executing actually
var total2=2(3);
console.log(total2);
totalAND to the variableplusOne-- no, it does nothing toplusOne. Why would it? IsplusOneon the left hand side of an assignment?plusOneis a reference to the function.plusOneexpression is evaluated to a function-object (objects, including functions, are first-class values) which is then invoked with the()operator. This is also exactly why/how an IIFE works in JavaScript.