Breaking forEach loop in JavaScript

forEach loop in JavaScript cannot be broken by using break statement. Here are some ways to break a forEach loop: Using Array. length: arr=[1,2,3,4,5]; arr.forEach(function(value){    if(value == 2){        arr.length=0;    }    console.log(‘value=’, value);    }); Using splice: arr=[1,2,3,4,5]; arr.forEach(function(value,index){    if(value == 2){        arr.splice(index+1, arr.length);    }… Continue reading Breaking forEach loop in JavaScript

Published
Categorized as JavaScript

‘toReversed’ method in JavaScript

The method ‘toReversed()’ is added to JavaScript in ECMAScript 2023 (ES 13) version. The advantage of this method over ‘reverse()’ is that it doesn’t change the original array. It reverses the array and returns a new array. Below is the example code for it and the output: x=[1,2,3,4,5]; x.reverse(); console.log(‘The reverse method applied and the… Continue reading ‘toReversed’ method in JavaScript

Published
Categorized as JavaScript Tagged