2_javascript_functions_451d74321f
2_javascript_functions_451d74321f
2 Javascript Functions
Declaring a Function
The syntax to declare a function is:
function nameOfFunction () {
// function body
}
Calling a Function
function nameOfFunction () {
// function body
}
Function Parameters
function add(a, b) {
console.log(a + b);
}
Function Return
Arrow Functions
The arrow function is one of the features introduced in the ES6 version of
JavaScript. It allows you to create functions in a cleaner way compared to
regular functions. For example,This function
// function expression
let multiply = function(x, y) {
return x * y;
}
can be written as
In the ES6 version, you can pass default values in the function parameters.
For example,
function sum(x, y = 5) {
// take sum
// the value of y is 5 if not passed
console.log(x + y);
sum(5); // 10
sum(5, 15); // 20
In the above example, if you don't pass the parameter for y , it will take 5 by
default.
First-class citizens
Be assigned to variables.
Be passed as arguments.
console.log(applyOperation(3, 5, add)); // 8
console.log(applyOperation(3, 5, multiply)); // 15
2. Create a method to take the average of three numbers, and then convert it
to an arrow function.