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