JavaScript Basics
Write a JavaScript program to display Hello World
Description: Write a JavaScript program that prints “Hello, World!” in the console.
Hint:
- Use
console.log(). - Text must be inside quotes.
JavaScript Hello World program
console.log("Hello, World!");
Output:
Hello, World!
How it works
console.log()prints a message in the browser console (press F12 to open it).- The text is a string, so it is written in quotes.
Read JavaScript Basic Program.
Write a JavaScript program to add two numbers
Description: Store two numbers in variables and print their sum.
Hint:
- Use
letorconstfor the variables. - Use the
+operator.
JavaScript program to add two numbers
const a = 12;
const b = 8;
const sum = a + b;
console.log(`Sum of ${a} and ${b} is ${sum}`);
Output:
Sum of 12 and 8 is 20
How it works
- The
+operator adds two numbers. - A template literal with
${ }puts the values inside the text.
Read JavaScript Operators. If the numbers come from prompt() or an input box, convert them with Number() first, otherwise "12" + "8" gives "128".
Write a JavaScript program to swap two variables
Description: Swap the values of two variables a and b.
Hint:
- The old way uses a third variable.
- The modern way uses array destructuring.
JavaScript program to swap two variables
let a = 10;
let b = 20;
console.log("Before:", a, b);
[a, b] = [b, a];
console.log("After:", a, b);
Output:
Before: 10 20
After: 20 10
How it works
- The right side
[b, a]makes a new array with the two values. - The left side
[a, b]destructures it back into the variables, so they are swapped in one line.
Read ES6 Features.
Write a JavaScript program to check even or odd number
Description: Check whether a number is even or odd.
Hint:
- A number is even if the remainder after dividing by 2 is 0.
JavaScript program for even or odd
function checkEvenOdd(n) {
if (n % 2 === 0) {
return n + " is even";
}
return n + " is odd";
}
console.log(checkEvenOdd(4));
console.log(checkEvenOdd(7));
Output:
4 is even
7 is odd
How it works
%gives the remainder:4 % 2is 0 and7 % 2is 1.- The function returns a text for each case.
Read Conditional Statements.
Write a JavaScript program to find the largest of three numbers
Description: Find the largest of the three numbers 25, 78 and 42.
Hint:
- Compare with
ifand&&. - Or use
Math.max().
JavaScript program for the largest of three numbers
const a = 25, b = 78, c = 42;
let largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
console.log("Largest:", largest);
console.log("Using Math.max:", Math.max(a, b, c));
Output:
Largest: 78
Using Math.max: 78
How it works
- The first
ifchecks ifais at least as big as both others. Math.max()does the same in one call.
Write a JavaScript program to check leap year
Description: Check if a year is a leap year.
Hint:
- Divisible by 4 and not by 100, or divisible by 400.
JavaScript program to check a leap year
function isLeap(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
for (const y of [2024, 1900, 2000, 2023]) {
console.log(y + (isLeap(y) ? " is a leap year" : " is not a leap year"));
}
Output:
2024 is a leap year
1900 is not a leap year
2000 is a leap year
2023 is not a leap year
How it works
- 1900 is divisible by 100 but not by 400, so it is not a leap year.
- The ternary operator picks the text.