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