1setTimeout(function () {
2 console.log('Execute later after 1 second')
3}, 1000);Code language: JavaScript (javascript)
1function caller(otherFunction) {
2 otherFunction(2);
3 }
4caller(function(x) {
5 console.log(x);
6});
1// Regular function, called explicitly by name .aka “named function”
2function multiply(a, b){
3 var result = a * b;
4 console.log(result);
5}
6multiply(5, 3);
7
8// Anonymous function stored in variable.
9// Invoked by calling the variable as a function
10// Anonymous functions don't have a name,
11// so the parentheses appears right after “function” keyword.
12var divided = function() {
13 var result = 15 / 3;
14 console.log("15 divided by 4 is " + result);
15}
16divided();
17
18// Immediately Invoked Function Expression.
19// Immediately invoked function expressions are anonymous functions
20// with another parentheses pair at the end to trigger them,
21// all wrapped inside parentheses.
22// Runs as soon as the browser finds it:
23(function() {
24 var result = 20 / 10;
25 console.log("20 divided by 10 is " + result);
26}())
27