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