Skip to main content

Posts

Showing posts from March 26, 2023

Use of ForEach in Java Script & Node JS - For Each Loop

USE of ForEach in JavaScript  let friends = [ 'Vasu' , 'Mohit' , 'Sunil' , 'Abhishek' ]; // using forEach friends . forEach ( myFunction ); function myFunction ( item ) {     console . log ( item )   } . .. Output Vasu Mohit Sunil Abhishek // For changing in each element  or if you want to something random or same value with each Elecment of array -  Use below code  let friends = [ 'Vasu' , 'Mohit' , 'Sunil' , 'Abhishek' ]; // using forEach friends . forEach ( myFunction ); function myFunction ( item , index , arr ) {       arr [ index ] = 'Hello ' + item ; } console . log ( friends ) .. Output : - [ 'Hello Vasu' , 'Hello Mohit' , 'Hello Sunil' , 'Hello Abhishek' ] FOR vs ForEach both can be used to manipulate Array   const oldArray = [ 'item1' , 'item2' , 'item3' ]; const newArray = []; // using for loop for ( let i = 0 ; i < oldArr...