Arrow Functions
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 51 of 77.
Arrow functions provide a shorter way to write functions in JavaScript. They are especially handy for short, simple functions.
An arrow function expression has a shorter syntax compared to function expressions and does not have its own bindings to the this, arguments, super, or new.target keywords. Arrow functions are always anonymous.
Here's the basic syntax of an arrow function:
(param1, param2) => {
// Function body
// ...
return value; // Optional return statement
}If the function has only one parameter, you can omit the parentheses around the parameter list. If the function body has only one expression, you can omit the curly braces and the return keyword. The result of the expression will be implicitly returned.
Here's an example of a traditional function expression:
let add = function(a, b) {
return a + b;
};Here's the same function written as an arrow function:
let add = (a, b) => a + b;In this example, the parentheses around the parameters are kept because there are two parameters. The curly braces and the return keyword are omitted because the function body has only one expression.
Here's another example with no parameters:
let greet = () => console.log("Hello!");
greet(); // Output: Hello!In this case, an empty pair of parentheses is used to indicate that the function has no parameters.
Arrow functions are often used when you need to pass a short function as an argument to another function.
Challenge
EasyCreate an arrow function named calculateBMI that takes two parameters, weight (in kilograms) and height (in meters), and returns the Body Mass Index (BMI). The formula for BMI is: weight / (height * height).
For example:
console.log(calculateBMI(70, 1.75)); // Output: 22.857142857142858
console.log(calculateBMI(80, 1.8)); // Output: 24.691358024691358Cheat sheet
Arrow functions provide a shorter syntax for writing functions in JavaScript. They are always anonymous and don't have their own this binding.
Basic syntax:
(param1, param2) => {
// Function body
return value;
}For single expressions, you can omit curly braces and return:
let add = (a, b) => a + b;For single parameters, parentheses are optional:
let square = x => x * x;For no parameters, use empty parentheses:
let greet = () => console.log("Hello!");Try it yourself
// Type your code below
// Don't change the lines below
console.log(calculateBMI(70, 1.75));
console.log(calculateBMI(80, 1.8));This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False