Functions Flashcards
Function
function showMessage() {
alert( ‘Hello everyone!’ );
}
function sum(a, b) {
return a + b;
}
Function is a value
let sayHi = function() {
alert( “Hello” );
};
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
A function is a special value, in the sense that we can call it like sayHi().
Regular values like strings or numbers represent the data.
A function can be perceived as an action.
Semicolon at end
The semicolon would be there for a simpler assignment, such as let sayHi = 5;, and it’s also there for a function assignment.
Arrow Functions
Simpler form than function expressions
let sum = (a, b) => a + b;
Multiline Arrow Functions
let sum = (a, b) => { // the curly brace opens a multiline function
let result = a + b;
return result; // if we use curly braces, then we need an explicit “return”
};