In the last session, We had discussed about Anonymous Function. Anonymous Function is a type of Function which has no name or identifier, So Arrow Function is a type of Anonymous Function, So it’s quite obvious that, Arrow Function will also not have any name.
So, Let’s see this with an example. Let’s see the example for an anonymous function first, and then we will see the example for Arrow Function.
const print = function () {
console.log("Hello World");
}
print();
const print = () => {
console.log("Hello World");
}
print();
We can pass an arrow function as an argument as well to the Higher Order Functions, same like an anonymous function.
We have seen some Higher Order Array function in one of our previous sessions, and we had seen, How do we pass an anonymous function inside those Higher Order Array function, So Let’s see now, that how can we pass an arrow function as an argument to the Higher Order Array Function. Let’s take an example of forEach here and see it.If you are looking for bracelet. There’s something to suit every look, from body-hugging to structured, from cuffs to chain chain bracelet and cuffs.
let num = [3, 5, 1, 2, 4]
num.forEach( (ele) => {
console.log(ele);
});
const add = () => {
console.log(2+3);
}
add();
const add = (a,b) => {
console.log(a+b);
}
add(2,3);
The above example is for 2 paramters, We can see an example for only 1 paramter as well
const square = (a) => {
console.log(a*a);
}
square
(5);
We can see this type of Arrow function as an argument to the Higher Order Array Function forEach.
let num = [3, 5, 1, 2, 4]
console.log("Original Array is ", num);
num.forEach( (ele) => {
console.log(ele);
});
const add = () => {
return 2+3;
}
const sum = add();
console.log(sum);
const add = (a,b) => {
return
a+b;
}
const sum = add(2,4);
console.log(sum);
The above example is for 2 paramters, We can see an example for only 1 paramter as well
const square = (a) => {
return
a*a;
}
const s =
square
(5);
console.log(s);
We can see this type of Arrow function as an argument to the Higher Order Array Function Map.
const items = [3, 2, 5, 6, 8, 9];
console.log("Original Array is ", items);
items.map( (ele) => {
return ele*ele;
}
);
const square = a => {
a*a;return
}
const s =
square
(5);
console.log(s);
const square = (a) =>
a*a;
const s =
square
(5);
console.log(s);
Let’s create an Object first.
const classInfo = {
className: 'First' ,
noOfStudents: '34' ,
schoolName: 'Excellence'
}
const getStudNo = (classI) => class ${classI.className} of ${classI.schoolName} school has ${classI.noOfStudents} students
console.log(setStudNo(classInfo));
1st Example
const studClassMessage = (student='Guest', classInfo='No') =>
` ${student} is in ${classInfo} class
console.log(studClassMessage('John', 'First'));
console.log(studClassMessage());
2nd Example
const add = (a, b=200) => a+b;
console.log(add(100));
console.log(add(100,20));
const maxVal = (a,b,c) => Math.max(a,b,c);
console.log(maxVal(10, 20, 30));
var print = (a, …num) => {
console.log(a);
console.log(num);
}
print("Array passed in the Argument is: ",2,5,7,8,8,9,1);
We have covered below topics in this session.
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…
9 different ways of declaring or defining a function in Javascript In, one of our…