JavaScript Conditional Programs
Learn how JavaScript makes decisions with if, else and switch. Check even or odd numbers, leap years, grades and more, with clear output for each program.
5 programs · open one, then the next
Start with Check Even or Odd- 01Check Even or OddBeginnerCheck if a number is even or odd in JavaScript using n % 2 === 0. Covers if-else, the ternary operator and negative numbers, with explanation and output.Open example →js// Method 1: Using if-else let number = 7; if (number % 2 === 0) {
- 02Largest Among Three NumbersBeginnerFind the largest of three numbers in JavaScript with if-else, Math.max and the spread operator. Shows why >= handles ties, with explanation and output.Open example →js// Method 1: Using if-else let a = 10, b = 25, c = 15; let largest;
- 03Vowel/Consonant CheckBeginnerCheck if a letter is a vowel or consonant in JavaScript with if-else, includes() and switch. Covers case, non-letters and Y. Explanation and output.Open example →js// Method 1: Using if-else function checkVowelConsonant(char) { // Convert to lowercase for case-insensitive check char = char.toLowerCase();
- 04Leap Year CheckBeginnerCheck for a leap year in JavaScript with the 4, 100 and 400 rules. Three methods, test years 2000, 1900, 2023 and 2024, plus a clear explanation and output.Open example →js// Leap year rules: // 1. Divisible by 4 → Leap year // 2. BUT if divisible by 100 → NOT leap year // 3. UNLESS divisible by 400 → Leap year
- 05Grade CalculatorBeginnerBuild a grade calculator in JavaScript that turns marks into A, B, C, D or F with if-else and switch(true). Rejects invalid marks, with explanation and output.Open example →js// Grade system: // 90-100: A // 80-89: B // 70-79: C