JS5 ClassNotes
JS5 ClassNotes
Block of code that performs a specific task, can be invoked whenever needed
Functions in JS
Function Definition Function Call
}
Arrow Functions
Compact way of writing a function
return a + b;
}
Let‘s Practice
Qs. Create a function using the “function” keyword that takes a String as an argument &
returns the number of vowels in the string.
console.log(val);
})
Let‘s Practice
Qs. For a given array of numbers, print the square of each value using the forEach loop.
Some More Array Methods
Map
Creates a new array with the results of some operation. The value its callback returns are
used to form new array
return val * 2;
})
Some More Array Methods
Filter
})
Some More Array Methods
Reduce
Performs some operations & reduces the array to a single value. It returns
that single value.
Let‘s Practice
Qs. We are given array of marks of students. Filter our of the marks of students that scored 90+.
Qs. Take a number n as input from user. Create an array of numbers from 1 to n.
Use the reduce method to calculate sum of all numbers in the array.
Use the reduce method to calculate product of all numbers in the array.