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