1.
Swap Two Numbers Without Using a Third Variable
let a = 5, b = 10;
a = a + b; // 15
b = a - b; // 5
a = a - b; // 10
console.log(a, b); // Output: 10 5
2. Check if a Number is Even or Odd
let num = 7;
if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
// Output: Odd
3. Find the Largest of Three Numbers
let a = 12, b = 25, c = 8;
let max = Math.max(a, b, c);
console.log(max); // Output: 25
4. Reverse a String
let str = "hello";
let reversed = str.split('').reverse().join('');
console.log(reversed); // Output: "olleh"
5. Check if a String is a Palindrome
let str = "madam";
let isPalindrome = str === str.split('').reverse().join('');
console.log(isPalindrome); // Output: true
6. Factorial of a Number
let num = 5;
let fact = 1;
for (let i = 1; i <= num; i++) {
fact *= i;
console.log(fact); // Output: 120
7. Fibonacci Series up to N Terms
let n = 6;
let a = 0, b = 1;
for (let i = 0; i < n; i++) {
console.log(a);
let temp = a + b;
a = b;
b = temp;
// Output: 0 1 1 2 3 5
8. Sum of Digits of a Number
let num = 123;
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
console.log(sum); // Output: 6
9. Count the Number of Vowels in a String
let str = "javascript";
let count = 0;
for (let char of str.toLowerCase()) {
if ("aeiou".includes(char)) {
count++;
console.log(count); // Output: 3
10. Check if a Number is Prime
let num = 7;
let isPrime = true;
if (num < 2) isPrime = false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
isPrime = false;
break;
console.log(isPrime); // Output: true