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