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