Math and Number Utilities

Key takeaway: Practice JavaScript with 102+ runnable code examples — variables, arrays, strings, objects, math, dates, and DOM — all with expected output to verify instantly.

JavaScript is a programming language that makes web pages interactive. These examples are for front-end developers, bootcamp students, self-taught coders, and anyone preparing for JS interviews.

Last updated: October 2026 · 102 examples

Quick Start

  1. Pick an example from the sidebar or mobile menu
  2. Study the code and expected output
  3. Run it in our online JS compiler or browser console
  4. Modify the code to experiment

Who is this for: Front-end developers, bootcamp students, interview candidates, and self-taught coders building JavaScript skills. Use this when: you need quick reference code, want to practice for interviews, or learn JS concepts hands-on.

Related:JS Projects · JS Tutorial · JS Compiler · TypeScript Examples · Python Examples · MDN JavaScript Docs

JavaScript Program to Print the Fibonacci Sequence

Generate the first N Fibonacci numbers.

Source Code
function fib(n){const a=[0,1]; for(let i=2;i<n;i++){a[i]=a[i-1]+a[i-2];} return a.slice(0,n);} console.log(fib(10));
Expected Output

JavaScript Program to Find the Square Root

Compute square root using Math.sqrt.

Source Code
const n=81; console.log(Math.sqrt(n));
Expected Output

JavaScript Program to Check Prime Number

Check divisibility up to sqrt(n).

Source Code
function isPrime(n){if(n<2)return false; for(let i=2;i*i<=n;i++){if(n%i===0)return false;} return true;} console.log(isPrime(29));
Expected Output

JavaScript Program to Print All Prime Numbers in an Interval

List primes in a range.

Source Code
function isPrime(n){if(n<2)return false; for(let i=2;i*i<=n;i++){if(n%i===0)return false;} return true;} const out=[]; for(let n=10;n<=50;n++){if(isPrime(n))out.push(n);} console.log(out.join(", "));
Expected Output

JavaScript Program to Find the Factorial of a Number

Iterative factorial.

Source Code
function fact(n){let f=1; for(let i=2;i<=n;i++){f*=i;} return f;} console.log(fact(5));
Expected Output

JavaScript Program to Display the Multiplication Table

Multiplication table for a number.

Source Code
const n=5; for(let i=1;i<=10;i++){ console.log(n+" x "+i+" = "+(n*i)); }
Expected Output

JavaScript Program to Check Armstrong Number

Check if sum of powered digits equals number.

Source Code
function isArmstrong(n){const s=String(n); const p=s.length; let sum=0; for(const ch of s){sum+=Math.pow(Number(ch),p);} return sum===n;} console.log(isArmstrong(153));
Expected Output

JavaScript Program to Find Armstrong Number in an Interval

List Armstrong numbers in range.

Source Code
function isA(n){const s=String(n); const p=s.length; let sum=0; for(const ch of s){sum+=Math.pow(Number(ch),p);} return sum===n;} const out=[]; for(let n=100;n<=999;n++){if(isA(n))out.push(n);} console.log(out);
Expected Output

JavaScript Program to Find the Sum of Natural Numbers

Sum 1..n.

Source Code
function sumN(n){let s=0; for(let i=1;i<=n;i++){s+=i;} return s;} console.log(sumN(100));
Expected Output

JavaScript Program to Find HCF or GCD

Euclidean algorithm.

Source Code
function gcd(a,b){while(b){[a,b]=[b,a%b];} return a;} console.log(gcd(48,18));
Expected Output

JavaScript Program to Find LCM

LCM using GCD.

Source Code
function gcd(a,b){while(b){[a,b]=[b,a%b];} return a;} function lcm(a,b){return Math.abs(a*b)/gcd(a,b);} console.log(lcm(12,18));
Expected Output

JavaScript Program to Find Sum of Natural Numbers Using Recursion

Recursive sum.

Source Code
function sumN(n){return n<=1?n: n+sumN(n-1);} console.log(sumN(10));
Expected Output

JavaScript Program to Display Fibonacci Sequence Using Recursion

Recursive Fibonacci.

Source Code
function fib(n){return n<2?n: fib(n-1)+fib(n-2);} console.log(Array.from({length:10},(_,i)=>fib(i)));
Expected Output

JavaScript Program to Find Factorial of Number Using Recursion

Recursive factorial.

Source Code
function fact(n){return n<=1?1:n*fact(n-1);} console.log(fact(6));
Expected Output

Keep Practicing

Use the online compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

Yes, run them instantly. Copy the code into our online JS compiler or your browser console (F12 → Console). Output uses console.log.

ES6+ (modern JavaScript). All examples work in Chrome, Firefox, Safari, and Edge. Arrow functions, template literals, and destructuring are used where appropriate.

Yes, absolutely. All examples are free to copy, modify, and share. Experimentation is the fastest way to learn JavaScript.

Start with basics, then build up. Begin with Hello World → variables → arithmetic → control flow → functions → strings → arrays → objects.

Learn JavaScript by Practicing Examples

Hands-on practice is the fastest way to understand JavaScript. Each example above focuses on a single concept—from strings, arrays, and objects to math utilities, dates, loops, and functions. Try editing variables, adding conditions, or refactoring logic, then observe the output changes instantly.

Use our tools to deepen your learning: JavaScript Compiler to run snippets, and the JavaScript Tutorial for guided theory and practice.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with simple, readable programs.

Practical Patterns

Practice common tasks like formatting strings, manipulating arrays, and working with dates.

Grow Skills

Advance to objects, classes, and asynchronous patterns such as promises and async/await.