![]() |
In JavaScript, iterating through numbers or any kind of iterable data structures is a common programming task. The ability to repeat a certain block of code multiple times allows you to perform operations on a set of data. We can iterate through an array of numbers and return a string message if the sum of the numbers is negative. Table of Content Using JavaScript For LoopThe for loop in JavaScript is a control flow statement that allows you to repeatedly execute a block of code based on a specified condition. The for loop has three parts: initialization of a counter variable, the condition itself, and the update section. Syntax:for(initialization; condition; updation){}
Example: The below code uses a JavaScript function with a for loop to iterate through numbers and return a string message if the sum is negative. Javascript
Output
The sum of the numbers is -7, and is negative. The sum of the numbers is 9, and is positive. Using JavaScript For Of LoopThe for of loop in JavaScript provides a concise and easy-to-read syntax for iterating through the values of an iterable, eliminating the need for explicit index management. This loop has namely two parts: the variable which is assigned the value of each element of the iterable on each iteration, and the iterable itself. Example: The below code uses a JavaScript function with a for of loop to iterate through numbers and return a string message if the sum is negative. Javascript
Output
The sum of the numbers is -7, and is negative. The sum of the numbers is 9, and is positive. Using JavaScript While LoopThe while loop in JavaScript is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition evaluates to true. Syntax:while(consitionalExpression){ Example: The below code uses a JavaScript function with the while loop to iterate through numbers and return a string message if the sum is negative. Javascript
Output
The sum of the numbers is -7, and is negative. The sum of the numbers is 9, and is positive. Using JavaScript Array forEach() MethodThe forEach() method is a higher-order function available for arrays. It allows you to iterate over the elements of an array and perform a specified operation by passing a callback function to operate on each element. Syntax:arrayName.forEach(()=>{});
Example: The below code uses a JavaScript function with the Array forEach() method to iterate through numbers and return a string message if the sum is negative. Javascript
Output
The sum of the numbers is -7, and is negative. The sum of the numbers is 9, and is positive. Using JavaScript Array reduce() MethodThe Javascript Array reduce() method is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator. Syntax:arrayName.reduce(()=>{});
Example: The below code uses a JavaScript function with the Array reduce() method to iterate through numbers and return a string message if the sum is negative. Javascript
Output
The sum of the numbers is -7, and is negative. The sum of the numbers is 9, and is positive. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |