JavaScript Program Examples


Here, are some Important JavaScript programs that cover a range of concepts and can help you improve your JavaScript skills:

1. Write a Program to print - Hello, World in JavaScript

This is a basic program that prints "Hello, World!" to the console.

Program

<!DOCTYPE html>
<html>
<head>
<title></title> 
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2> 
<script>
document.write("Hello, World!");
</script>
</body>
</html>

Output

JavaScript Programs

2. Factorial Program

This program calculates the factorial of a given number. The factorial function says to multiply all the whole numbers from the chosen number down to one. In more mathematical terms, the factorial of a number (n!) is equal to n(n-1).

For example, if you want to calculate the factorial for four, you would write:

4! = 4 x 3 x 2 x 1 = 24.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function factorial(num) {
if (num === 0) {
return 1;
} else {
return num * factorial(num - 1);
}
}
document.write("Factorial Value="+factorial(5)); // 120
</script>
</body>
</html>

Output

JavaScript Programs

3. Fibonacci Series

This program generates the Fibonacci series up to a given number. The fibonacci series numbers are given as: 0, 1, 1, 2, 3, 5, 8, 13, 21. In a Fibonacci series, every term is the sum of the preceding two terms, starting from 0 and 1 as the first and second terms.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function fibonacci(num) {
let a = 0, b = 1, temp;
while (num >= 0) {
temp = a; a = a + b; b = temp; num--;
} return b;
}
document.write("fibonacci Value=" +fibonacci(8)); // 21
</script>
</body>
</html>

Output

JavaScript Programs

4. Palindrome

This program checks whether a given string is a palindrome. A palindrome number is a number that remains the same when digits are reversed.

For example, the number 12321, racecar are a palindrome number, but 1451 is not a palindrome number.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function isPalindrome(str) {
const reversed = str.split("").reverse().join("");
return str === reversed;
}
document.write("Palindrome Check =" + isPalindrome("racecar")); // true
</script>
</body>
</html>

Output

JavaScript Programs

5. Anagram

This program checks whether two given strings are anagrams.

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function isAnagram(str1, str2) {
const sorted1 = str1.toLowerCase().split("").sort().join("");
const sorted2 = str2.toLowerCase().split("").sort().join("");
return sorted1 === sorted2;
}
document.write("Anagram Check =" + isAnagram("listen", "silent")); // true

</script>
</body>
</html>

Output

JavaScript Programs

6. Sum of array

This program calculates the sum of an array of numbers. For Example array [1, 2, 3, 4, 5] sum is 15.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function sumArray(arr) {
return arr.reduce((acc, curr) => acc + curr, 0);
}
document.write("Array Sum =" + sumArray([1, 2, 3, 4, 5])); // 15 
</script>
</body>
</html>

Output

JavaScript Programs

7. Average of array

This program calculates the average of an array of numbers. Average Array is  Sum of array elemnt/ Lenth of array. For Example 15/3=5.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function averageArray(arr) {
const sum = arr.reduce((acc, curr) => acc + curr, 0);
return sum / arr.length;
}
document.write("Array Average =" + averageArray([1, 2, 3, 4, 5])); // 3 
</script>
</body>
</html>

Output

JavaScript Programs

8. Prime number

This program checks whether a given number is prime. a prime number is only divisible by 1 and itself. If you divide a prime number by a number other than 1 and itself, you will get a non-zero remainder. For Example these are the prime numbers 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>

function isPrime(num) {
if (num <= 1) {
return false;
} for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
} return true;
}
document.write("Check Prime Number = " + isPrime(7)); // true 
</script>
</body>
</html>

Output

JavaScript Programs

9. Reverse string

This program reverses a given string. For Example Raj reverse sting is jar.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function reverseString(str) {
return str.split("").reverse().join("");
}
document.write("Reverse String = " + reverseString("Rohatash")); 
</script>
</body>
</html>

Output

JavaScript Programs

10. Capitalize first letter

This program capitalizes the first letter of each word in a given string. For Example we take hello world it will produce following output- Hello World.

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function capitalize(str) {
return str.replace(/(^|\s)\S/g, function (match) {
return match.toUpperCase();
});
}
document.write("capitalize String = " + capitalize("hello world")); // "Hello World"
</script>
</body>
</html>

Output

JavaScript Programs

11. Remove duplicates from an Array

This program removes duplicates from an array. Here is an array - [1, 2, 2, 3, 3, 3, 4, 5, 5] which have 2 and 3 duplicate numbers. So program will remove duplicate number and give the following Output- [1, 2, 3, 4, 5]

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function removeDuplicates(arr) {
const unique = [];
for (let i = 0; i < arr.length; i++) {
if (!unique.includes(arr[i])) {
unique.push(arr[i]);
}
} return unique;
} document.write("Remove Duplicates = " + removeDuplicates([1, 2, 2, 3, 3, 3, 4, 5, 5])); // [1, 2, 3, 4, 5]
</script>
</body>
</html>

Output

JavaScript Programs

12. Merge arrays

This program merges two arrays into one. That menas we have two array [1, 2, 3] and  [4, 5, 6]. The program merge these two array in single array like that -  [1, 2, 3, 4, 5, 6]

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function mergeArrays(arr1, arr2) {
return [...arr1, ...arr2];
}
document.write("Merge Array = " + mergeArrays([1, 2, 3], [4, 5, 6])); // [1, 2, 3, 4, 5, 6] 
</script>
</body>
</html>

Output

JavaScript Programs

13. Sort array

This program sorts an array of numbers in ascending order. That menas we have one array [5, 3, 2, 4, 1]. The program sorts this array in ascending order like that -  [1, 2, 3, 4, 5]

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function sortArray(arr) {
return arr.sort((a, b) => a - b);
}
document.write("Sort Array = " + sortArray([5, 3, 2, 4, 1])); // [1, 2, 3, 4, 5] 
</script>
</body>
</html>

Output

JavaScript Programs

14. Reverse array

This program reverses an array. For Example we have an array [1, 2, 3, 4, 5]. The program convert it in reverse order like that - [5, 4, 3, 2, 1]

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function reverseArray(arr) {
return arr.reverse();
}
document.write("Reverse Array = " + reverseArray([1, 2, 3, 4, 5])); // [5, 4, 3, 2, 1] 
</script>
</body>
</html>

Output

JavaScript Programs

15. Find Duplicates

This program finds duplicate elements in an array. Here is an array - [1, 2, 2, 3, 3, 3, 4, 5, 5] which have 2, 3 and 5 duplicate numbers. So program will find duplicate number and give the following Output- [2, 3, 5]

Program

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - JavaScript Programes</h2>
<script>
function findDuplicates(arr) {
const duplicates = [];

for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i && !duplicates.includes(arr[i])) {
duplicates.push(arr[i]);
}
}
return duplicates;
}
document.write("Duplicate Record = " + findDuplicates([1, 2, 2, 3, 3, 3, 4, 5, 5])); // [2, 3, 5] 
</script>
</body>
</html>

Output

JavaScript Programs
Prev Next