0% found this document useful (0 votes)
144 views

JS5 ClassNotes

The document provides an overview of functions in JavaScript, including traditional function definitions and arrow functions. It explains the use of the forEach loop for iterating over arrays, as well as array methods like map, filter, and reduce for manipulating data. Additionally, it includes practice questions to reinforce the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

JS5 ClassNotes

The document provides an overview of functions in JavaScript, including traditional function definitions and arrow functions. It explains the use of the forEach loop for iterating over arrays, as well as array methods like map, filter, and reduce for manipulating data. Additionally, it includes practice questions to reinforce the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Functions in JS

Block of code that performs a specific task, can be invoked whenever needed
Functions in JS
Function Definition Function Call

function functionName( ) { functionName( );

//do some work

function functionName( param1, param2 ...) {

//do some work

}
Arrow Functions
Compact way of writing a function

const functionName = ( param1, param2 ...) => {

//do some work

const sum = ( a, b ) => {

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.

Qs. Create an arrow function to perform the same task.


forEach Loop in Arrays
arr.forEach( callBackFunction )

CallbackFunction : Here, it is a function to execute for each element in the array

*A callback is a function passed as an argument to another function.

arr.forEach( ( val ) => {

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

arr.map( callbackFnx( value, index, array ) )

let newArr = arr.map( ( val ) => {

return val * 2;

})
Some More Array Methods
Filter

Creates a new array of elements that give true for a condition/filter.


Eg: all even elements

let newArr = arr.filter( ( ( val ) => {

return val % 2 === 0;

})
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.

You might also like