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