Functions
Learn about JavaScript function syntax, passing data to functions, the return keyword, ES6 arrow functions, and concise body syntax.
StartKey Concepts
Review core concepts you need to learn to master this subject
Arrow Functions (ES6)
Functions
Anonymous Functions
Function Expressions
Function Parameters
return
Keyword
Function Declaration
Calling Functions
Arrow Functions (ES6)
Arrow Functions (ES6)
// Arrow function with two arguments
const sum = (firstParam, secondParam) => {
return firstParam + secondParam;
};
console.log(sum(2,5)); // Prints: 7
// Arrow function with no arguments
const printHello = () => {
console.log('hello');
};
printHello(); // Prints: hello
// Arrow functions with a single argument
const checkWeight = weight => {
console.log(`Baggage weight : ${weight} kilograms.`);
};
checkWeight(25); // Prints: Baggage weight : 25 kilograms.
// Concise arrow functions
const multiply = (a, b) => a * b;
console.log(multiply(2, 30)); // Prints: 60
Arrow function expressions were introduced in ES6. These expressions are clean and concise. The syntax for an arrow function expression does not require the function
keyword and uses a fat arrow =>
to separate the parameter(s) from the body.
There are several variations of arrow functions:
- Arrow functions with a single parameter do not require
()
around the parameter list. - Arrow functions with a single expression can use the concise function body which returns the result of the expression without the
return
keyword.
What you'll create
Portfolio projects that showcase your new skills
Rock, Paper, or Scissors
It's time to build fluency in JavaScript fundamentals. In this next Pro Project, we're going to practice conditionals in JavaScript so you can hone your skills and feel confident taking them to the real world. Why? Given a certain input you want to return a certain output. If this than that. What's next? Games, conditionals, more JavaScript. You got this!
Sleep Debt Calculator
It's time to build fluency in JavaScript fundamentals. In this next Pro Project, we're going to practice functions in JavaScript so you can hone your skills and feel confident taking them to the real world. Why? Functions help us build separation of concerns so our code isn't one long function. What's next? Calculators, sleep awareness, more JavaScript. You got this!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory