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