ES6 Arrow Functions
Arrow functions are a new addition to JavaScript that let us create inline functions without using the function
and return
keywords. They are simply a shorter syntax version of the normal JavaScript functions.
JavaScript function:
1234let myFunc = function () { return 1 + 6; } console.log(myFunc());
ES6 function:
let myFunc = () => {
return 1 + 6;
}
If the function has only one statement and returns a value, then we can remove the braces and the return keyword, hence the resultant code will be:
let myFunc = () => 1 + 6;
We can also pass arguments in the ES6 inline functions, using the syntax:
12let myFunc = (a, b) => a + b console.log(myFunc(5, 4));
Task
Create an inline function named threeProduct
that takes in three numbers / arguments namely a
, b
, c
, and returns the product of those numbers.
Call the function inside a console.log function and pass 1
, 2
and 9
as arguments.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 2.7
ES6 Arrow Functions
Pyyhkäise näyttääksesi valikon
Arrow functions are a new addition to JavaScript that let us create inline functions without using the function
and return
keywords. They are simply a shorter syntax version of the normal JavaScript functions.
JavaScript function:
1234let myFunc = function () { return 1 + 6; } console.log(myFunc());
ES6 function:
let myFunc = () => {
return 1 + 6;
}
If the function has only one statement and returns a value, then we can remove the braces and the return keyword, hence the resultant code will be:
let myFunc = () => 1 + 6;
We can also pass arguments in the ES6 inline functions, using the syntax:
12let myFunc = (a, b) => a + b console.log(myFunc(5, 4));
Task
Create an inline function named threeProduct
that takes in three numbers / arguments namely a
, b
, c
, and returns the product of those numbers.
Call the function inside a console.log function and pass 1
, 2
and 9
as arguments.
Kiitos palautteestasi!