JavaScript Examples
Classic JavaScript programs in modern JavaScript (let/const, arrow functions, array methods), each with its exact output, a step-by-step explanation, common mistakes and exercises. Every output was produced by running the program.
Each example opens in the online JavaScript Compiler with one click, so you can change it and run it straight away, or press Visualize step by step to watch the program run line by line with its variables and call stack. Everything runs in your browser, with nothing to install.
Also available: Python Examples.
Basics
Input, arithmetic and if/else: the first programs everyone writes.
- JavaScript Program to Add Two Numbers
Add two numbers in JavaScript, read them as input, and see why "2" + "3" gives "23": convert strings with Number() before adding them.
Beginner - JavaScript Program to Swap Two Variables
Swap the values of two variables in JavaScript with array destructuring ([a, b] = [b, a]), and with the temporary-variable method used in most languages.
Beginner - JavaScript Program to Check if a Number is Even or Odd
Check whether a number is even or odd in JavaScript with the remainder operator (%), and learn why -3 % 2 is -1 and how to handle it safely.
Beginner - JavaScript Program to Find the Largest of Three Numbers
Find the largest of three numbers in JavaScript with if/else if/else and the && operator, then in one line with Math.max() and the spread operator.
Beginner - JavaScript Program to Check Leap Year
Check whether a year is a leap year in JavaScript with the full rule (divisible by 4, but centuries only when divisible by 400), and verify it with Date.
Beginner
Numbers
Primes, factorials, Fibonacci and other loops over digits and divisors.
- JavaScript Program to Check Prime Number
Check whether a number is prime in JavaScript by testing divisors up to its square root, step by step, plus the bugs that make 1 or 9 look prime.
Beginner - JavaScript Program to Print Prime Numbers in a Range
Print all prime numbers between two numbers in JavaScript with a labelled nested loop, then with the much faster Sieve of Eratosthenes using a typed array.
Intermediate - JavaScript Program to Find the Factorial of a Number
Find the factorial of a number in JavaScript with a loop and with recursion, and use BigInt for exact results beyond 18!, where normal numbers lose precision.
Beginner - JavaScript Program to Print the Fibonacci Sequence
Print the Fibonacci sequence in JavaScript with a loop and destructuring, see why naive recursion is slow, and fix it with memoization in a Map.
Beginner - JavaScript Program to Check Armstrong Number
Check whether a number is an Armstrong number in JavaScript by raising each digit to the number of digits with **, and list all of them below 10,000.
Beginner - JavaScript Program to Find the Sum of Digits of a Number
Find the sum of the digits of a number in JavaScript with a while loop using % and Math.trunc(), and in one line with split() and reduce(), negatives included.
Beginner - JavaScript Program to Find the GCD (HCF) of Two Numbers
Find the greatest common divisor (GCD or HCF) of two numbers in JavaScript with Euclid's algorithm, recursively and with a loop, and use it to get the LCM.
Beginner - JavaScript Program to Convert Decimal to Binary
Convert a decimal number to binary in JavaScript by repeated division by 2, then with toString(2) and padStart(), and convert back with parseInt(text, 2).
Beginner
Strings
Reversing, counting and comparing text.
- JavaScript Program to Check if a String is a Palindrome
Check whether a string is a palindrome in JavaScript, ignoring case, spaces and punctuation with a regular expression, plus a two-pointer version.
Beginner - JavaScript Program to Reverse a String
Reverse a string in JavaScript with split, reverse and join, with a for loop, and with Array.from() for emoji, then reverse the order of words in a sentence.
Beginner - JavaScript Program to Count the Vowels in a String
Count the vowels in a string in JavaScript with a loop and includes(), count each vowel in an object, and do it in one line with a regex and match().
Beginner - JavaScript Program to Check if Two Strings are Anagrams
Check whether two strings are anagrams in JavaScript by sorting their letters, or by counting letters in a Map, ignoring case, spaces and punctuation.
Beginner - JavaScript Program to Count Word Frequency in a String
Count how often each word appears in a text in JavaScript with a Map, ignore case and punctuation with a regex, and sort the words by frequency.
Intermediate
Arrays and algorithms
Searching, sorting and working with arrays.
- JavaScript Program to Find the Largest Element in an Array
Find the largest number in a JavaScript array with a loop, with Math.max(...array) and with reduce(), and find the second largest even when the maximum repeats.
Beginner - JavaScript Program to Remove Duplicates from an Array
Remove duplicate values from a JavaScript array with a Set, with filter() and indexOf(), and remove duplicate objects by a key such as an id.
Beginner - JavaScript Program for Bubble Sort
Sort an array with bubble sort in JavaScript, step through every swap in the visualizer, stop early when sorted, and use sort() with a compare function.
Intermediate - JavaScript Program for Binary Search
Implement binary search in JavaScript on a sorted array, watch the search range halve in the visualizer, and compare it with indexOf() and findIndex().
Intermediate
Patterns and small projects
Nested loops and complete little programs.
- JavaScript Program to Print a Star Pattern (Pyramid)
Print star patterns in JavaScript: a right triangle, a centred pyramid and an inverted triangle, using nested loops and String.prototype.repeat().
Beginner - JavaScript Program to Display the Multiplication Table
Print the multiplication table of any number in JavaScript with a for loop and padStart() for aligned columns, then a full grid with nested loops.
Beginner - JavaScript Program to Make a Simple Calculator
Build a simple calculator in JavaScript that reads lines like "12 * 4", picks the operation from an object of functions and reports bad input without eval().
Intermediate