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