Assigned to Variables

When a function is assigned to a variable it is often called a function expression or function literal.
When a function is assigned to a variable it is not hoisted.
You do have to include a semicolon at the end.


The function name can be omitted but it is best practice to always include a name so it appears on the call stack when debugging.
This function is assigned to a global variable called myVar.

var myVar = function() { 
   return arguments[0] + arguments[1];
}
myVar();


let varMyFunction = function() { 
    console.log('Anonymous function');
};
varMyFunction();


var AddNumbers = function() { 
   return arguments[0] + arguments[1];
}
alert(AddNumbers(1,2)


var IsEven = function(num) { 
   return num % 2 === 0;
}
console.log(IsEven(2));




© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext