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