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