Software Development Exam  >  Software Development Questions  >  What will be the output of the following code... Start Learning for Free
What will be the output of the following code?
let arr = [1, 2, 3, 4, 5];
for (let element of arr) {
  if (element === 3) {
    break;
  }
  console.log(element);
}
  • a)
    1 2 3 4 5
  • b)
    1 2 4 5
  • c)
    1 2 4
  • d)
    The code contains an error
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following code?let arr = [1, 2, 3, 4, 5...
The for...of loop iterates over the elements of the array 'arr' and stops when it encounters the value 3.
Free Test
Community Answer
What will be the output of the following code?let arr = [1, 2, 3, 4, 5...
Code Explanation:
The given code declares an array `arr` with values `[1, 2, 3, 4, 5]`. It then uses a `for...of` loop to iterate over each element in the array. Inside the loop, it checks if the current element is equal to 3. If it is, the loop is terminated using the `break` statement. If the element is not equal to 3, it is logged to the console using `console.log`.

Code Execution:
1. The loop starts with the first element of the array, which is 1.
2. Since 1 is not equal to 3, it is logged to the console.
3. The loop moves to the next element, which is 2.
4. Again, 2 is not equal to 3, so it is logged to the console.
5. The loop moves to the next element, which is 3.
6. As 3 is equal to 3, the `break` statement is executed, terminating the loop.
7. Hence, the loop does not continue to the remaining elements of the array (4 and 5).
8. The output of the code execution will be:
```
1
2
3
```

Conclusion:
The given code will output the numbers 1, 2, and 3. The loop terminates when it encounters the element 3, so the remaining elements (4 and 5) are not logged to the console. Therefore, the correct answer is option A: 1 2 3 4 5.
Attention Software Development Students!
To make sure you are not studying endlessly, EduRev has designed Software Development study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Software Development.
Explore Courses for Software Development exam

Top Courses for Software Development

What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer?
Question Description
What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? for Software Development 2024 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? covers all topics & solutions for Software Development 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following code?let arr = [1, 2, 3, 4, 5];for (let element of arr) { if (element === 3) { break; } console.log(element);}a)1 2 3 4 5b)1 2 4 5c)1 2 4d)The code contains an errorCorrect answer is option 'A'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev