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