String Utilities and Text Processing
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
- Pick an example from the sidebar or mobile menu
- Study the code and expected output
- Run it in our online JS compiler or browser console
- 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 Encode a String to Base64
Use btoa.
console.log(btoa("hello"));JavaScript Program to Check Whether a String is Palindrome or Not
Compare reversed string.
function isPal(s){const t=s.toLowerCase().replace(/[^a-z0-9]/g,""); return t===t.split("").reverse().join("");} console.log(isPal("Racecar"));JavaScript Program to Replace Characters of a String
Replace chars using regex.
const s="hello"; console.log(s.replace(/l/g,"x"));
JavaScript Program to Reverse a String
Reverse a string.
const s="JavaScript"; console.log(s.split("").reverse().join(""));JavaScript Program to Convert the First Letter of a String into UpperCase
Capitalize first letter.
function cap(s){return s.charAt(0).toUpperCase()+s.slice(1);} console.log(cap("hello"));JavaScript Program to Count the Number of Vowels in a String
Use regex to count.
function vowels(s){const m=s.match(/[aeiou]/gi); return m? m.length:0;} console.log(vowels("Hello World"));JavaScript Program to Replace All Occurrences of a String
Use replaceAll.
const s="a a a"; console.log(s.replaceAll("a","b"));JavaScript Program to Create Multiline Strings
Use template literals.
const s=`line1\nline2`; console.log(s);
JavaScript Program to Generate Random String
Pick random chars.
const chars="abcdefghijklmnopqrstuvwxyz0123456789"; let s=""; for(let i=0;i<8;i++){s+=chars[Math.floor(Math.random()*chars.length)];} console.log(s);JavaScript Program to Trim a String
Remove surrounding whitespace.
console.log(" hello ".trim());JavaScript Program to Convert Objects to Strings
Use JSON.stringify.
const o={a:1}; console.log(JSON.stringify(o));JavaScript Program to Check Whether a String Contains a Substring
Use includes.
console.log("hello world".includes("world"));JavaScript Program to Compare Two Strings
Use localeCompare.
console.log("apple".localeCompare("banana"));JavaScript Program to Replace all Instances of a Character in a String
Use replaceAll.
console.log("balloon".replaceAll("l","x"));JavaScript Program to Replace All Line Breaks with
Replace \n with space.
const s="a\nb\nc"; console.log(s.replace(/\r?\n/g," "));
Keep Practicing
Use the online compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.
FAQ
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.