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