Practice

JavaScript Exercises for Beginners

15 practical exercises with solutions: variables, conditionals, loops, functions, arrays. Try to solve each one yourself, then open the answer and check it.

🧩 15 exercises 🎯 Level: beginner 🔄 Updated: 2026
Contents
  1. Variables and data types
  2. Conditionals (if / else)
  3. Loops (for)
  4. Functions
  5. Arrays

1. Variables and data types

Exercise 1

Declare a variable age and set it to your age. Log a phrase like "I am 25 years old" to the console, using the variable's value.

Show solution
const age = 25;
console.log(`I am ${age} years old`);
Exercise 2

Declare two variables — a and b — with any numbers. Log their sum, difference, and product to the console.

Show solution
const a = 10;
const b = 4;
console.log(a + b); // sum
console.log(a - b); // difference
console.log(a * b); // product
Exercise 3

You have a string "42". Convert it to a number and log the result of adding 8 to it.

Show solution
const str = "42";
const num = Number(str);
console.log(num + 8); // 50

2. Conditionals (if / else)

Exercise 4

Given a number age. If it's 18 or greater, log "Access granted" to the console, otherwise log "Access denied".

Show solution
const age = 20;
if (age >= 18) {
  console.log("Access granted");
} else {
  console.log("Access denied");
}
Exercise 5

Given a number, determine and log to the console whether it's even or odd.

Show solution
const num = 7;
if (num % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd");
}
Exercise 6

Given a score (a number from 0 to 100). Log a letter grade: 90+ — "A", 70-89 — "B", below 70 — "C".

Show solution
const score = 82;
if (score >= 90) {
  console.log("A");
} else if (score >= 70) {
  console.log("B");
} else {
  console.log("C");
}

3. Loops (for)

Exercise 7

Log all numbers from 1 to 10 to the console using a for loop.

Show solution
for (let i = 1; i <= 10; i++) {
  console.log(i);
}
Exercise 8

Calculate and log the sum of all numbers from 1 to 100.

Show solution
let sum = 0;
for (let i = 1; i <= 100; i++) {
  sum += i;
}
console.log(sum); // 5050
Exercise 9

Log only the even numbers from 1 to 20 to the console.

Show solution
for (let i = 1; i <= 20; i++) {
  if (i % 2 === 0) {
    console.log(i);
  }
}

4. Functions

Exercise 10

Write a function square that takes a number and returns its square.

Show solution
function square(n) {
  return n * n;
}
console.log(square(5)); // 25
Exercise 11

Write a function isEven that takes a number and returns true if it's even, and false otherwise.

Show solution
function isEven(n) {
  return n % 2 === 0;
}
console.log(isEven(4)); // true
console.log(isEven(7)); // false
Exercise 12

Write a function greet that takes a name and returns the string "Hello, [name]!".

Show solution
function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet("Alex")); // "Hello, Alex!"

5. Arrays

Exercise 13

Given the array of numbers [3, 7, 1, 9, 4]. Find and log the largest number to the console.

Show solution
const nums = [3, 7, 1, 9, 4];
const max = Math.max(...nums);
console.log(max); // 9
Exercise 14

Given the array ["apple", "banana", "cherry"]. Log each element to the console along with its number, starting from 1.

Show solution
const fruits = ["apple", "banana", "cherry"];
fruits.forEach((fruit, index) => {
  console.log(`${index + 1}. ${fruit}`);
});
Exercise 15

Given the array of numbers [1, 2, 3, 4, 5]. Create a new array where every number is multiplied by 2, using map.

Show solution
const nums = [1, 2, 3, 4, 5];
const doubled = nums.map((n) => n * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

What's next

Enjoyed these exercises?

The JavaScript course has 40+ lessons and dozens of exercises with homework after every topic, from variables all the way to async code and mini-projects.