Function Definition
function printKing() {
console.log('avi biter');
}
printKing();
Function Value
just like other values:
const nisim = 42;
const shlomo = 'gimel yafit';
const david = false;
you can assign a function value to a variable
const printSinger = function() {
return 'shimi tavori';
};
We can this function with the variable just like a function defined regularly:
const printKing = function() {
console.log('avi biter');
};
printKing();
Warning
assignment of function value is not hoisted as regular function definition.
printKing(); // works fine
function printKing() {
console.log('avi biter');
}
printKing(); // error!
// Uncaught ReferenceError: can't access lexical
// declaration 'printKing' before initialization
const printKing = function() {
console.log('avi biter');
};
Callback
a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
const greeting = function(name) {
alert('Hello ' + name);
};
function processUserInput(callback) {
const name = prompt("Please enter your name.");
callback(name);
}
processUserInput(greeting);
Use Alias
instead of definning the function and giving it a name we can pass it’s definition as an argument to the processUserInput function.
function processUserInput(callback) {
const name = prompt("Please enter your name.");
callback(name);
}
processUserInput(function(name) {
alert('Hello ' + name);
});
Arrow Function
Multiple Lines + Multiple Parameters
const doSomething = function(p1, p2, p3) {
// first line
// second line
// ...
};
arrow version
const doSomething = (p1, p2, p3) => {
// first line
// second line
// ...
};
Multiple Lines + Single Parameter
const doSomething = function(p1) {
// first line
// second line
// ...
};
arrow version
const doSomething = (p1) => {
// first line
// second line
// ...
};
with a single parameter the parenthesis can be omitted
const doSomething = p1 => {
// first line
// second line
// ...
};
Single Return Line + Multiple Parameters
const getSomething = function(p1, p2, p3) {
return 'buldozer';
};
arrow version
const getSomething = (p1, p2, p3) => {
return 'buldozer';
};
shorthand: with a single return line the block parenthesis and the return
word
can be omitted.
const getSomething = (p1, p2, p3) => 'buldozer';
Single Return Line + Single Parameter
const getDouble = function(num) {
return 2 * num;
};
arrow version
const getDouble = (num) => {
return 2 * num;
};
shorthand: as explained before
const getDouble = num => 2 * num;