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