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