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