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