What are the following looping structures are available in javascripts ?
- AOption (a)
for, foreach
- BOption (b)
foreach, while loop
- COption (c)
do-while loop, foreach
- DOption (d)
for, while loop
View answer & explanationHide answer & explanation
Correct answer
for, foreach
Why this answer is correct
The correct answer is Option 1: for, foreach
Key Points
- for loop: ✅ The for loop is a widely used looping structure in JavaScript. It allows you to iterate through a block of code a specific number of times.
- foreach loop: ✅ The foreach loop is used to iterate over array elements. It applies a callback function to each element of the array.
- while loop: ❌ The while loop is another valid looping structure in JavaScript, but it is not listed in Option 1, making it incorrect in this context.
- do-while loop: ❌ The do-while loop is a valid looping structure in JavaScript, but it is not part of Option 1.
Additional Information
- for loop: The syntax of a for loop is: for (initialization; condition; increment/decrement) { // code }. It is used for precise iteration over a known range.
- foreach loop: The foreach loop is specifically designed for array traversal. Example syntax: array.forEach((element) => { // code }).
- while loop: The while loop runs as long as the condition evaluates to true. Syntax: while (condition) { // code }.
- do-while loop: The do-while loop runs the code at least once before checking the condition. Syntax: do { // code } while (condition);.
Hence, the correct answer is: Option 1: for, foreach