JavaScript Functions
What is a Function?
A function is a block of code designed to perform a particular task. A function is executed when "something" invokes it (calls it).
Function Expressions
A function can also be defined using an expression. A function expression can be stored in a variable.
const square = function(number) {
return number * number;
};
let x = square(4); // x gets the value 16
Arrow Functions
Arrow functions (introduced in ES6) allow for a shorter syntax for writing function expressions.
{} and the return keyword.
Parameters and Arguments
Parameters are the names listed in the function
definition.
Arguments are the real values passed to the
function.
Summary
- Functions are reusable blocks of code.
- Functions can return values using the
returnkeyword. - Variables declared inside a function are local to the function.
- Arrow functions provide a concise syntax for writing functions.
Quick Quiz
Which syntax is correct for an arrow function?
Enjoying these tutorials?