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