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