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