JavaScript Worksheet: If and For Loops
Code tip (Check, then count): if (condition) { … } else { … }: only one of the two parts runs. for (let i = 0; i < 5; i++) runs for i = 0, 1, 2, 3, 4 — 5 times. With i <= n it includes n; with i = i + 2 it goes up in twos.
NameDateTime takenScore ___ / 40
- 1.🔁 When does the else part run? (a) when if is false (b) when if is true (c) always (d) never
-
2.🔁 How many times does this code print “Well done”?
for (let i = 1; i < 7; i++) {
console.log("Well done");
} -
3.🔁 What does this code print?
let marks = 41;
if (marks < 29) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
4.🔁 How many times does this code print “Well done”?
for (let i = 3; i <= 12; i++) {
console.log("Well done");
} -
5.🔁 How many numbers does this code print?
for (let i = 0; i < 16; i = i + 4) {
console.log(i);
} -
6.🔁 What does this code print?
let age = 1;
if (age >= 31) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
7.🔁 How many times does this code print “Welcome”?
for (let i = 1; i < 5; i++) {
console.log("Welcome");
} -
8.🔁 How many numbers does this code print?
for (let i = 0; i < 5; i = i + 4) {
console.log(i);
} - 9.🔁 What does i++ do? (a) doubles i (b) prints i (c) adds 1 to i (d) takes 1 from i
-
10.🔁 How many times does this code print “Hi”?
for (let i = 1; i <= 8; i++) {
console.log("Hi");
} -
11.🔁 What is the LAST number this code prints?
for (let i = 0; i < 16; i = i + 3) {
console.log(i);
} -
12.🔁 How many times does this code print “Hi”?
for (let i = 3; i <= 7; i++) {
console.log("Hi");
} -
13.🔁 What does this code print?
let score = 13;
if (score >= 7) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
14.🔁 How many times does this code print “Well done”?
for (let i = 1; i <= 5; i++) {
console.log("Well done");
} -
15.🔁 How many numbers does this code print?
for (let i = 0; i < 18; i = i + 4) {
console.log(i);
} - 16.🔁 Which word runs code only when something is true? (a) let (b) if (c) for (d) log
- 17.🔁 Which loop repeats code a set number of times? (a) if (b) let (c) else (d) for
-
18.🔁 How many times does this code print “Well done”?
for (let i = 0; i <= 5; i++) {
console.log("Well done");
} -
19.🔁 What does this code print?
let marks = 26;
if (marks >= 40) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
20.🔁 What does this code print?
let age = 7;
if (age > 28) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
21.🔁 How many times does this code print “Hello”?
for (let i = 2; i < 5; i++) {
console.log("Hello");
} -
22.🔁 What does this code print?
let score = 10;
if (score > 37) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
23.🔁 How many times does this code print “Welcome”?
for (let i = 1; i <= 4; i++) {
console.log("Welcome");
} -
24.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 2) {
console.log(i);
} -
25.🔁 What is the LAST number this code prints?
for (let i = 0; i < 12; i = i + 4) {
console.log(i);
} -
26.🔁 How many times does this code print “Hello”?
for (let i = 2; i < 8; i++) {
console.log("Hello");
} -
27.🔁 How many times does this code print “Welcome”?
for (let i = 1; i <= 10; i++) {
console.log("Welcome");
} -
28.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + 3;
}
console.log(total); -
29.🔁 What is the LAST number this code prints?
for (let i = 0; i < 5; i = i + 3) {
console.log(i);
} -
30.🔁 What does this code print?
let marks = 39;
if (marks > 9) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
31.🔁 How many times does this code print “Welcome”?
for (let i = 2; i < 7; i++) {
console.log("Welcome");
} -
32.🔁 What does this code print?
let temp = 23;
if (temp >= 40) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Cool (b) Hot -
33.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + i;
}
console.log(total); -
34.🔁 How many times does this code print “Welcome”?
for (let i = 1; i <= 3; i++) {
console.log("Welcome");
} -
35.🔁 How many times does this code print “Welcome”?
for (let i = 3; i < 10; i++) {
console.log("Welcome");
} -
36.🔁 What does this code print?
let temp = 29;
if (temp >= 11) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
37.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 5; i++) {
total = total + i;
}
console.log(total); -
38.🔁 What does this code print?
let marks = 27;
if (marks < 19) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
39.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 3; i++) {
total = total + 3;
}
console.log(total); -
40.🔁 How many times does this code print “Well done”?
for (let i = 2; i < 7; i++) {
console.log("Well done");
}
Answer key
- when if is false
- 6
- Try again
- 10
- 4
- child
- 4
- 2
- adds 1 to i
- 8
- 15
- 5
- You win
- 5
- 5
- if
- for
- 6
- Try again
- child
- 3
- Play again
- 4
- 6
- 8
- 6
- 10
- 30
- 3
- Pass
- 5
- Cool
- 28
- 3
- 7
- Hot
- 15
- Try again
- 9
- 5
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids