Sept 15, 2022
Functions are a series of grouped statements that can be called by an assigned identifer. They have the capability of accepting input in the form of parameters and can return a value as output.
Functions are great in places where we expect to need to repeat the same task multiple times and avoid duplicated code.
function sayHello(name) {
console.log("Hello, " + name);
}
sayHello('Don'); // outputs "Hello, Don"
Functions can also return values:
function area(width, height) {
return width * height;
}
let rectArea = area(5, 6); // rectArea is 30
Arrow functions are a compact alternative to normal functions.
function square(num) {
return num * num;
}
could become
let square = (num) => num * num;