Start Coding with JavaScript
5 Beginner Exercises to Kickstart Your Journey
8/3/20231 min read
JavaScript is a powerful and versatile programming language used extensively for web development. If you're new to coding and eager to begin your journey with JavaScript, you've come to the right place! In this post, we'll introduce you to five beginner-level exercises to help you grasp the fundamentals of JavaScript programming. Let's dive in!
Start Coding with JavaScript: 5 Beginner Exercises to Kickstart Your Journey
Exercise 1: Hello, World!
// Write a simple JavaScript program to print "Hello, World!" to the console.
console.log("Hello, World!");
Exercise 2: Basic Math Operations
// Create a JavaScript function that takes two numbers as parameters and returns their sum.
function addNumbers(num1, num2) {
return num1 + num2;
}
// Test the function with sample values.
const result = addNumbers(5, 10); console.log("Sum:", result);
Exercise 3: Find the Maximum Number
// Write a JavaScript function that takes an array of numbers and returns the maximum value.
function findMax(numbers) {
return Math.max(...numbers);
}
// Test the function with a sample array.
const numbersArray = [10, 45, 3, 78, 22];
const maxNumber = findMax(numbersArray);
console.log("Maximum number:", maxNumber);
Exercise 4: Check Even or Odd
// Create a JavaScript function that checks if a given number is even or odd.
function checkEvenOrOdd(number) {
if (number % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
// Test the function with a sample number.
const sampleNumber = 15;
console.log(sampleNumber, "is", checkEvenOrOdd(sampleNumber));
Exercise 5: Convert Celsius to Fahrenheit
// Write a JavaScript function that converts Celsius to Fahrenheit. function celsiusToFahrenheit(celsius) {
return (celsius * 9/5) + 32;
}
// Test the function with a sample Celsius value.
const celsiusValue = 30;
console.log(celsiusValue + "°C is equal to", celsiusToFahrenheit(celsiusValue) + "°F");
Conclusion:
Congratulations! You've completed five beginner-level JavaScript exercises that cover basic concepts like printing to the console, performing math operations, working with arrays, and creating functions. These exercises provide a solid foundation for further exploring the world of JavaScript programming.
As you progress, challenge yourself with more complex tasks and projects, and don't forget to practice regularly to reinforce your skills. Happy coding and enjoy your exciting journey into the world of JavaScript!