Boost your JavaScript skills with beginner-friendly practice exercises! This collection of JavaScript practice exercises covers variables, functions, conditional statements, arrays, loops, and objects.
From simple tasks like printing strings to more advanced challenges like calculating factorials or finding the longest word, these exercises will help you gain hands-on experience and strengthen your understanding of JavaScript. Whether you’re new to programming or looking to reinforce your skills, these exercises provide a perfect opportunity to practice and grow as a JavaScript developer.
1. Hello, World!: Write a script that prints “Hello, World!” to the console.
console.log("Hello, World!");
2. Sum of Two Numbers: Write a function that takes two numbers as parameters and returns their sum.
function sumNumbers(a, b) {
return a + b;
}
console.log(sumNumbers(3, 5)); //8
3. Even or Odd: Write a function that takes a number as a parameter and returns whether it’s even or odd.
function checkEvenOrOdd(number) {
if (number % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(checkEvenOrOdd(7)); // Odd
4. Reverse a String: Write a function that takes a string as a parameter and returns the reverse of that string.
function reverseString(str) {
return str.split("").reverse().join("");
}
console.log(reverseString("Hello"));
5. FizzBuzz: Write a program that prints the numbers from 1 to 100. But for multiples of three, print “Fizz” instead of the number, and for the multiples of five, print “Buzz”. For numbers that are multiples of both three and five, print “FizzBuzz”.
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
6. Find the Largest Number: Write a function that takes an array of numbers as a parameter and returns the largest number in the array.
function findLargestNumber(numbers) {
let largest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}
console.log(findLargestNumber([5, 2, 9, 1, 7]));
7. Factorial: Write a function that takes a number as a parameter and returns its factorial.
function factorial(number) {
if (number === 0 || number === 1) {
return 1;
}
let result = 1;
for (let i = 2; i <= number; i++) {
result *= i;
}
return result;
}
console.log(factorial(5));
8. Palindrome: Write a function that takes a string as a parameter and returns whether it’s a palindrome (reads the same forward and backward).
function isPalindrome(str) {
const reversed = str.split("").reverse().join("");
return str === reversed;
}
console.log(isPalindrome("madam"));
9. Fibonacci Sequence: Write a function that takes the number n as a parameter and prints the first n numbers in the Fibonacci sequence.
function fibonacciSequence(n) {
const sequence = [0, 1];
for (let i = 2; i < n; i++) {
const nextNumber = sequence[i - 1] + sequence[i - 2];
sequence.push(nextNumber);
}
return sequence;
}
console.log(fibonacciSequence(10));
10. Find the Longest Word: Write a function that takes a sentence as a parameter and returns the longest word in the sentence.
function findLongestWord(sentence) {
const words = sentence.split(" ");
let longestWord = words[0];
for (let i = 1; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}
console.log(findLongestWord("The quick brown fox jumps over the lazy dog"));
JavaScript practice exercises for beginners
Here’s a collection of JavaScript practice exercises suitable for beginners:
Variables and Data Types:
- Create a variable and assign it a string value. Print the variable to the console.
- Create two variables and assign them different numerical values. Add the variables together and print the result.
- Create a variable and assign it a boolean value. Print the variable to the console.
// 1
let greeting = "Hello, World!";
console.log(greeting);
// 2
let num1 = 5;
let num2 = 10;
let sum = num1 + num2;
console.log(sum);
// 3
let isTrue = true;
console.log(isTrue);
Functions:
- Write a function that takes two numbers as parameters and returns their product.
- Write a function that takes a string as a parameter and prints it to the console.
- Write a function that calculates the area of a rectangle, given its length and width.
// 1
function multiplyNumbers(num1, num2) {
return num1 * num2;
}
let result = multiplyNumbers(3, 4);
console.log(result);
// 2
function printString(str) {
console.log(str);
}
printString("Hello, JavaScript!");
//3
function calculateRectangleArea(length, width) {
return length * width;
}
let area = calculateRectangleArea(5, 10);
console.log(area);
Conditional Statements:
- Write a function that checks if a number is positive, negative, or zero.
- Write a function that takes a number as a parameter and prints “Even” if it’s even and “Odd” if it’s odd.
- Write a function that compares two numbers and returns the larger one.
// 1
function checkNumber(num) {
if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
}
checkNumber(5);
// 2
function checkEvenOrOdd(num) {
if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
}
checkEvenOrOdd(7);
// 3
function findLargerNumber(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
let largerNumber = findLargerNumber(5, 10);
console.log(largerNumber);
Arrays:
- Create an array of numbers and print each element to the console.
- Write a function that takes an array of numbers as a parameter and returns the sum of all the numbers.
- Write a function that takes an array of strings and returns the longest string in the array.
// 1
let numbers = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// 2
function calculateSum(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
let totalSum = calculateSum([1, 2, 3, 4, 5]);
console.log(totalSum);
// 3
function findLongestString(strings) {
let longestString = "";
for (let i = 0; i < strings.length; i++) {
if (strings[i].length > longestString.length) {
longestString = strings[i];
}
}
return longestString;
}
let longest = findLongestString(["apple", "banana", "cherry"]);
console.log(longest);
Loops:
- Write a loop that prints the numbers from 1 to 10 to the console.
- Write a loop that calculates the factorial of a number.
- Write a loop that iterates through an array and prints each element to the console.
// 1
for (let i = 1; i <= 10; i++) {
console.log(i);
}
// 2
function calculateFactorial(num) {
let factorial = 1;
for (let i = 1; i <= num; i++) {
factorial *= i;
}
return factorial;
}
let factorialOf5 = calculateFactorial(5);
console.log(factorialOf5);
// 3
let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
Objects:
- Create an object representing a person with properties like name, age, and occupation. Print the values to the console.
- Write a function that takes an object as a parameter and prints all its properties and values.
// 1
let person = {
name: "John Doe",
age: 30,
occupation: "Engineer"
};
// Print the values to the console.
console.log("Name:", person.name);
console.log("Age:", person.age);
console.log("Occupation:", person.occupation);
// 2
function printObjectProperties(obj) {
for (let property in obj) {
console.log(property + ": " + obj[property]);
}
}
// Example usage
let person = {
name: "John Doe",
age: 30,
occupation: "Engineer"
};
printObjectProperties(person);
Output:
These exercises cover fundamental JavaScript concepts and provide opportunities to practice programming skills. Remember to start with simpler exercises and gradually progress to more complex ones. Enjoy coding and learning!
Do comment if you have any doubts or suggestions on this JS exercise.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version