1. Print "Hello, World!"
Description: A simple JavaScript program to print "Hello, World!" to the console.
console.log("Hello, World!");
2. Reverse a String Without Using Inbuilt Methods
Description: A program to reverse a string manually without using JavaScript’s inbuilt functions.
function reverseString(str) {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log(reverseString("hello"));
3. Evaluate Checkbox Selection
Description: A program that evaluates if a checkbox is checked or not.
function evaluateCheckbox() {
var checkbox = document.getElementById("myCheckbox");
if (checkbox.checked) {
alert("Checkbox is selected.");
} else {
alert("Checkbox is not selected.");
}
}
4. Using Five Events in JavaScript
Description: Demonstrating the use of different events (click, mouseover, mouseout, focus, blur).
document.getElementById("btn").onclick = function() {
alert("Button clicked!");
};
document.getElementById("btn").onmouseover = function() {
alert("Mouse over button!");
};
document.getElementById("inputField").onfocus = function() {
alert("Input field focused!");
};
5. Print Prime Numbers from 1 to 100
Description: A program to print all prime numbers from 1 to 100.
function printPrimes() {
for (let i = 2; i <= 100; i++) {
let isPrime = true;
for (let j = 2; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
console.log(i);
}
}
}
printPrimes();
6. Check if a Number is Palindrome
Description: A program to check if a number is a palindrome.
function isPalindrome(num) {
let original = num.toString();
let reversed = original.split('').reverse().join('');
return original === reversed;
}
console.log(isPalindrome(121)); // true
7. Find the Biggest of Three Numbers
Description: A program to find the biggest of three numbers.
function findBiggest(a, b, c) {
return Math.max(a, b, c);
}
console.log(findBiggest(10, 20, 30)); // 30
8. Calculate Days Left Until Christmas
Description: A program to calculate the number of days left until next Christmas.
function daysUntilChristmas() {
const today = new Date();
const christmas = new Date(today.getFullYear(), 11, 25);
if (today > christmas) christmas.setFullYear(christmas.getFullYear() + 1);
const diff = christmas - today;
const days = Math.ceil(diff / (1000 * 3600 * 24));
console.log(days + " days left until Christmas!");
}
daysUntilChristmas();
9. Multiply and Divide Two Numbers
Description: A program to calculate the multiplication and division of two numbers.
function multiplyDivide(a, b) {
console.log("Multiplication: " + (a * b));
console.log("Division: " + (a / b));
}
multiplyDivide(6, 3);
10. Palindrome Check for String
Description: A program to check whether a string is a palindrome.
function isPalindromeString(str) {
let reversed = str.split('').reverse().join('');
return str === reversed;
}
console.log(isPalindromeString("madam")); // true
11. Store Student Data Using Object
Description: A program to store student name, phone number, and marks using an object.
let student = {
name: "John Doe",
phone: "123-456-7890",
marks: 85
};
console.log(student);
12. Display Array Elements in Ascending and Descending Order
Description: A program to display elements of an array in ascending and descending order.
let arr = [3, 1, 4, 1, 5, 9];
arr.sort((a, b) => a - b);
console.log("Ascending: ", arr);
arr.sort((a, b) => b - a);
console.log("Descending: ", arr);
13. Check if a Number is Even or Odd
Description: A program to check whether a number is even or odd.
function isEvenOrOdd(num) {
return num % 2 === 0 ? "Even" : "Odd";
}
console.log(isEvenOrOdd(5)); // Odd
14. Display Welcome Message
Description: A simple program to display a welcome message.
alert("Welcome to JavaScript!");
15. Extract Values at Specific Indexes from Array
Description: A program to extract values at specific indexes from an array.
function extractValues(arr, indexes) {
return indexes.map(index => arr[index]);
}
console.log(extractValues([10, 20, 30, 40], [0, 2]));
16. Search for a Date Within a String
Description: A program to search for a date in a string.
function findDateInString(str) {
const regex = /\d{2}\/\d{2}\/\d{4}/;
return regex.exec(str);
}
console.log(findDateInString("The event is on 12/04/2025"));
17. Change Background Color Repeatedly
Description: A program to change the background color of the web page repeatedly.
let colors = ["red", "blue", "green", "yellow", "purple"];
let i = 0;
setInterval(function() {
document.body.style.backgroundColor = colors[i];
i = (i + 1) % colors.length;
}, 1000);
18. Generate Fibonacci Series
Description: A program to generate the Fibonacci series up to a specified limit.
function fibonacciSeries(n) {
let a = 0, b = 1;
let fibSeries = [a, b];
for (let i = 2; i < n; i++) {
let next = a + b;
fibSeries.push(next);
a = b;
b = next;
}
console.log(fibSeries);
}
fibonacciSeries(10);
19. Validate Login Page
Description: A program to validate a login page form.
function validateLogin() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (username === "" || password === "") {
alert("Both fields are required!");
return false;
}
return true;
}
20. Generate Armstrong Numbers Between 1 to 100
Description: A program to generate Armstrong numbers between 1 and 100.
function isArmstrong(num) {
let sum = 0;
let temp = num;
let digits = temp.toString().length;
while (temp > 0) {
let digit = temp % 10;
sum += Math.pow(digit, digits);
temp = Math.floor(temp / 10);
}
return sum === num;
}
for (let i = 1; i <= 100; i++) {
if (isArmstrong(i)) {
console.log(i);
}
}
21. Find the Factorial of a Given Number
Description: A program to find the factorial of a given number.
function factorial(num) {
let result = 1;
for (let i = 1; i <= num; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // 120
22. Find Maximum Between Two Numbers
Description: A program to find the maximum between two numbers.
function findMaximum(a, b) {
return a > b ? a : b;
}
console.log(findMaximum(10, 20)); // 20
23. Generate Fibonacci Series
Description: A program to generate the Fibonacci series up to a specified number using recursion.
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
for (let i = 0; i < 10; i++) {
console.log(fibonacci(i));
}
24. Validate a Form
Description: A program to validate a form where all fields must be filled out.
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
if (name === "" || email === "") {
alert("Please fill in all fields.");
return false;
}
return true;
}
25. Find the HCF or GCD of Two Numbers
Description: A program to calculate the highest common factor (HCF) or greatest common divisor (GCD) of two numbers.
function hcf(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(hcf(24, 36)); // 12
26. Display a Random Image on Button Click
Description: A program to display a random image from a list when a button is clicked.
function displayRandomImage() {
let images = [
"image1.jpg",
"image2.jpg",
"image3.jpg",
"image4.jpg"
];
let randomImage = images[Math.floor(Math.random() * images.length)];
document.getElementById("image").src = randomImage;
}
27. Find Largest Among Three Numbers
Description: A program to find the largest among three numbers.
function findLargest(a, b, c) {
return Math.max(a, b, c);
}
console.log(findLargest(15, 30, 25)); // 30
28. Validate Email ID
Description: A program to validate an email ID.
function validateEmail(email) {
let regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
return regex.test(email);
}
console.log(validateEmail("test@example.com")); // true
29. Display Student Details
Description: A program to display student details using an object.
let student = {
name: "Alice",
age: 21,
grade: "A"
};
console.log("Student Name: " + student.name);
console.log("Student Age: " + student.age);
console.log("Student Grade: " + student.grade);
30. Print the Current Window Contents
Description: A program to print the current window contents using window.print()
.
function printWindowContents() {
window.print();
}
31. Convert Decimal to Binary
Description: A program to convert a decimal number to binary.
function decimalToBinary(decimal) {
return decimal.toString(2);
}
console.log(decimalToBinary(10)); // 1010
32. Print Numbers from 1 to 10 Using a For Loop
Description: A program to print numbers from 1 to 10 using a for
loop.
for (let i = 1; i <= 10; i++) {
console.log(i);
}
33. Generate Prime Numbers from 1 to 50
Description: A program to print all prime numbers from 1 to 50.
function generatePrimes() {
for (let i = 2; i <= 50; i++) {
let isPrime = true;
for (let j = 2; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
console.log(i);
}
}
}
generatePrimes();
34. Remove Duplicate Items from an Array (Ignore Case Sensitivity)
Description: A program to remove duplicate items from an array, ignoring case sensitivity.
function removeDuplicates(arr) {
let uniqueArr = [];
arr.forEach(item => {
if (!uniqueArr.some(existingItem => existingItem.toLowerCase() === item.toLowerCase())) {
uniqueArr.push(item);
}
});
return uniqueArr;
}
console.log(removeDuplicates(["apple", "banana", "Apple", "banana"])); // ["apple", "banana"]
35. Develop an Arithmetic Calculator
Description: A basic arithmetic calculator program.
function calculate(a, b, operator) {
switch (operator) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return "Invalid operator";
}
}
console.log(calculate(10, 5, '+')); // 15
36. Print Fibonacci Series
Description: A program to print the Fibonacci series using a while
loop.
function fibonacciWhile(n) {
let a = 0, b = 1;
let count = 0;
while (count < n) {
console.log(a);
let next = a + b;
a = b;
b = next;
count++;
}
}
fibonacciWhile(10);
37. Display Whether the Number is Even or Odd
Description: A program to display whether a number is even or odd.
function checkEvenOdd(num) {
if (num % 2 === 0) {
console.log(num + " is Even");
} else {
console.log(num + " is Odd");
}
}
checkEvenOdd(7); // 7 is Odd
38. Rotate Array Elements to the Left
Description: A program to rotate the elements of an array to the left.
function rotateLeft(arr) {
let firstElement = arr.shift();
arr.push(firstElement);
return arr;
}
console.log(rotateLeft([1, 2, 3, 4])); // [2, 3, 4, 1]
39. Using Switch Case
Description: A program to demonstrate a switch case
statement.
let day = 2;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
40. Generate Fibonacci Series Using Do-While Loop
Description: A program to generate Fibonacci series using a do-while
loop.
function fibonacciDoWhile(n) {
let a = 0, b = 1, count = 0;
do {
console.log(a);
let next = a + b;
a = b;
b = next;
count++;
} while (count < n);
}
fibonacciDoWhile(10);
41. Calculate Multiplication of Two Numbers
Description: A program to calculate the multiplication of two numbers.
function multiply(a, b) {
return a * b;
}
console.log(multiply(5, 6)); // 30
42. Validate Form
Description: A program to validate a form where fields are required.
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
if (name === "" || email === "") {
alert("All fields must be filled out.");
return false;
}
return true;
}
43. Print the Contents of the Current Window
Description: A program to print the contents of the current window using window.print()
.
function printWindowContents() {
window.print();
}
44. Display Numbers from 1 to 50
Description: A program to display numbers from 1 to 50.
for (let i = 1; i <= 50; i++) {
console.log(i);
}
45. Check if a String Starts with "Java"
Description: A program to check if a string starts with the word "Java".
function startsWithJava(str) {
return str.startsWith("Java");
}
console.log(startsWithJava("JavaScript")); // true
console.log(startsWithJava("Python")); // false
46. Check if a Number is Even or Odd
Description: A program to check if a number is even or odd.
function isEvenOrOdd(num) {
if (num % 2 === 0) {
console.log(num + " is Even");
} else {
console.log(num + " is Odd");
}
}
isEvenOrOdd(10); // 10 is Even
isEvenOrOdd(7); // 7 is Odd
47. Display 1 to 100 Even Numbers
Description: A program to display all even numbers from 1 to 100.
for (let i = 1; i <= 100; i++) {
if (i % 2 === 0) {
console.log(i);
}
}
48. Display a Random Number Between Two Values
Description: A program to display a random number between two specified values.
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomNumber(1, 100)); // Random number between 1 and 100
49. Count the Number of Vowels in a String
Description: A program to count the number of vowels in a string.
function countVowels(str) {
let vowels = 'aeiouAEIOU';
let count = 0;
for (let i = 0; i < str.length; i++) {
if (vowels.indexOf(str[i]) !== -1) {
count++;
}
}
return count;
}
console.log(countVowels("Hello World")); // 3
50. Find the Length of an Array
Description: A program to find the length of an array.
let arr = [1, 2, 3, 4, 5];
console.log(arr.length); // 5
51. Find the First and Last Element of an Array
Description: A program to find the first and last elements of an array.
let arr = [10, 20, 30, 40, 50];
console.log("First element: " + arr[0]); // 10
console.log("Last element: " + arr[arr.length - 1]); // 50
52. Find the Sum of All Elements in an Array
Description: A program to find the sum of all elements in an array.
function sumArray(arr) {
return arr.reduce((acc, curr) => acc + curr, 0);
}
console.log(sumArray([1, 2, 3, 4, 5])); // 15
53. Reverse an Array
Description: A program to reverse the elements of an array.
let arr = [1, 2, 3, 4, 5];
let reversedArr = arr.reverse();
console.log(reversedArr); // [5, 4, 3, 2, 1]
54. Sort Array in Ascending Order
Description: A program to sort an array in ascending order.
let arr = [5, 1, 3, 7, 2];
arr.sort((a, b) => a - b);
console.log(arr); // [1, 2, 3, 5, 7]
55. Check if an Array Contains a Specific Element
Description: A program to check if an array contains a specific element.
let arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // true
console.log(arr.includes(6)); // false
56. Get the Index of an Element in an Array
Description: A program to get the index of an element in an array.
let arr = [10, 20, 30, 40, 50];
console.log(arr.indexOf(30)); // 2
console.log(arr.indexOf(60)); // -1 (not found)
57. Remove an Element from an Array
Description: A program to remove an element from an array by value.
let arr = [1, 2, 3, 4, 5];
let index = arr.indexOf(3);
if (index !== -1) {
arr.splice(index, 1);
}
console.log(arr); // [1, 2, 4, 5]
58. Find the Maximum Value in an Array
Description: A program to find the maximum value in an array.
let arr = [10, 20, 5, 30, 25];
console.log(Math.max(...arr)); // 30
59. Find the Minimum Value in an Array
Description: A program to find the minimum value in an array.
let arr = [10, 20, 5, 30, 25];
console.log(Math.min(...arr)); // 5
60. Check if a String is an Anagram of Another
Description: A program to check if two strings are anagrams.
function areAnagrams(str1, str2) {
let sortedStr1 = str1.split('').sort().join('');
let sortedStr2 = str2.split('').sort().join('');
return sortedStr1 === sortedStr2;
}
console.log(areAnagrams("listen", "silent")); // true
console.log(areAnagrams("hello", "world")); // false
0 Comments