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.🔁 How many times does this code print “Welcome”?
for (let i = 0; i <= 7; i++) {
console.log("Welcome");
} -
2.🔁 What does this code print?
let age = 3;
if (age > 34) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
3.🔁 What does this code print?
let marks = 33;
if (marks >= 12) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
4.🔁 What does this code print?
let score = 47;
if (score > 22) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
5.🔁 What is the LAST number this code prints?
for (let i = 0; i < 9; i = i + 3) {
console.log(i);
} - 6.🔁 Which word runs code only when something is true? (a) for (b) if (c) let (d) log
-
7.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + i;
}
console.log(total); -
8.🔁 What does this code print?
let age = 5;
if (age >= 8) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
9.🔁 What does this code print?
let temp = 47;
if (temp < 44) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
10.🔁 What does this code print?
let marks = 2;
if (marks > 9) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
11.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 5; i++) {
total = total + 4;
}
console.log(total); -
12.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 6; i++) {
total = total + i;
}
console.log(total); - 13.🔁 Which loop repeats code a set number of times? (a) if (b) else (c) let (d) for
-
14.🔁 How many numbers does this code print?
for (let i = 0; i < 7; i = i + 3) {
console.log(i);
} -
15.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 3) {
console.log(i);
} - 16.🔁 When does the else part run? (a) when if is true (b) when if is false (c) always (d) never
-
17.🔁 How many numbers does this code print?
for (let i = 0; i < 8; i = i + 4) {
console.log(i);
} -
18.🔁 How many times does this code print “Welcome”?
for (let i = 3; i < 9; i++) {
console.log("Welcome");
} -
19.🔁 What does this code print?
let score = 9;
if (score > 23) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
20.🔁 What is the LAST number this code prints?
for (let i = 0; i < 11; i = i + 2) {
console.log(i);
} -
21.🔁 What does this code print?
let score = 13;
if (score > 17) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again - 22.🔁 What does i++ do? (a) adds 1 to i (b) takes 1 from i (c) prints i (d) doubles i
-
23.🔁 How many times does this code print “Hi”?
for (let i = 2; i <= 11; i++) {
console.log("Hi");
} -
24.🔁 How many numbers does this code print?
for (let i = 0; i < 18; i = i + 2) {
console.log(i);
} -
25.🔁 What does this code print?
let temp = 26;
if (temp >= 19) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
26.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + 3;
}
console.log(total); -
27.🔁 What is the LAST number this code prints?
for (let i = 0; i < 17; i = i + 3) {
console.log(i);
} -
28.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total); -
29.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
30.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + i;
}
console.log(total); -
31.🔁 How many times does this code print “Well done”?
for (let i = 2; i <= 8; i++) {
console.log("Well done");
} -
32.🔁 What does this code print?
let age = 5;
if (age > 24) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
33.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + 5;
}
console.log(total); -
34.🔁 What does this code print?
let temp = 24;
if (temp >= 24) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Cool (b) Hot -
35.🔁 How many times does this code print “Hello”?
for (let i = 2; i <= 10; i++) {
console.log("Hello");
} -
36.🔁 How many numbers does this code print?
for (let i = 0; i < 6; i = i + 3) {
console.log(i);
} -
37.🔁 What is the LAST number this code prints?
for (let i = 0; i < 6; i = i + 4) {
console.log(i);
} -
38.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + 7;
}
console.log(total); -
39.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 2) {
console.log(i);
} -
40.🔁 What does this code print?
let marks = 49;
if (marks < 14) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass
Answer key
- 8
- child
- Pass
- You win
- 6
- if
- 10
- child
- Cool
- Try again
- 20
- 21
- for
- 3
- 5
- when if is false
- 2
- 6
- Play again
- 10
- Play again
- adds 1 to i
- 10
- 9
- Hot
- 12
- 15
- 55
- 36
- 45
- 7
- child
- 20
- Hot
- 9
- 2
- 4
- 28
- 8
- Try again
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids