0% found this document useful (0 votes)
1 views2 pages

Javascript Programs

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Javascript Programs

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

JavaScript Programs

Find Smallest Element in Array


let arr = [34, 12, 5, 67, 1];
let smallest = arr[0];
for (let num of arr) {
if (num < smallest) smallest = num;
}
document.writeln("Smallest element: " + smallest + "<br>");
console.log("Smallest element:", smallest);

Find Sum of Array Elements


let arr = [34, 12, 5, 67, 1];
let sum = 0;
for (let num of arr) sum += num;
document.writeln("Sum of elements: " + sum + "<br>");
console.log("Sum of elements:", sum);

Remove Duplicates from Array


let arr = [1, 2, 2, 3, 4, 4, 5];
let unique = [...new Set(arr)];
document.writeln("Array without duplicates: " + unique + "<br>");
console.log("Array without duplicates:", unique);

Check Palindrome Number


let num = parseInt(prompt("Enter a number:"));
let reversed = 0, temp = num;
while (temp > 0) {
reversed = reversed * 10 + (temp % 10);
temp = Math.floor(temp / 10);
}
if (reversed === num) {
document.writeln(num + " is a Palindrome number<br>");
console.log(num + " is a Palindrome number");
} else {
document.writeln(num + " is NOT a Palindrome number<br>");
console.log(num + " is NOT a Palindrome number");
}

Generate Random Number in Range


let min = parseInt(prompt("Enter minimum value:"));
let max = parseInt(prompt("Enter maximum value:"));
let random = Math.floor(Math.random() * (max - min + 1)) + min;
document.writeln("Random number: " + random + "<br>");
console.log("Random number:", random);

Using var in JavaScript


// Using var
var x = 10;
console.log("Initial value of x:", x);

var x = 20; // Redeclaration allowed


console.log("After redeclaration, x:", x);

x = 30; // Update allowed


console.log("After updating, x:", x);

Using let in JavaScript


// Using let
let y = 10;
console.log("Initial value of y:", y);

y = 20; // Update allowed


console.log("After updating, y:", y);

// let y = 30; // ❌ This will give an error: Identifier 'y' has already been declared

You might also like