Computer ScienceNEB 2081 (MCQs)
What is the correct syntax for a 'for loop' in JavaScript ? a) for (var i = 0; i < 5; i++) b) for (i = 0; i < 5; i++) c) for (var i = 0; i < 5) d) for (var i < 5; i++)
1Answer
The correct syntax for a for-loop in JavaScript is:
a) for (var i = 0; i < 5; i++) {}
Explanation:
A for loop in JavaScript follows this structure:
for (initialization; condition; increment/decrement) {
// Code to execute
}
var i = 0initializes the loop counter.i < 5is the condition that must be true for the loop to continue.i++increments the counter after each iteration.
Why not the others?
- b) Missing
var(declaration) → invalid in strict mode. - c) Missing increment/decrement → infinite loop.
- d) Incorrect syntax (comparison instead of initialization) → invalid.
// Example of a working for-loop:
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}
Discussion
Loading…
More Computer Science questions
Which of the following is the purpose of using primary key in database ? a) To uniquely identify a record b) To store duplicate record c) To backup data d) To…NEB 2082 (MCQs)1A company needs to modify an existing table by adding a new column for employee email addresses. For this, which SQL command should be used ? a) CREATE b)…NEB 2082 (MCQs)1Which protocol is used for secure communication over a computer network ? a) HTTP b) FTP c) HTTPS d) TelnetNEB 2082 (MCQs)1Which control structure in JS (Java Script) is used to execute a block of code repeatedly based on a given condition ? a) For loop b) if else c) switch case…NEB 2082 (MCQs)1Select the invalid variable name in PHP. a) $name b) $ name c) $1name d) $name123NEB 2082 (MCQs)1The statement int number ( int a); is a ... a) function call b) function definition c) function declaration d) function executionNEB 2082 (MCQs)1