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