Date and Time 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
- 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 Display Date and Time
Print current date/time.
console.log(new Date().toString());
JavaScript Program to Format the Date
Format with locale options.
const d=new Date(); console.log(d.toLocaleString("en-GB",{dateStyle:"full",timeStyle:"short"}));Javascript Program to Display Current Date
Use toLocaleDateString.
console.log(new Date().toLocaleDateString());
JavaScript Program to Compare The Value of Two Dates
Compare timestamps.
const a=new Date("2023-01-01"), b=new Date("2023-01-02"); console.log(a.getTime()===b.getTime());JavaScript Program to Create Countdown Timer
Simple countdown.
let s=5; const id=setInterval(()=>{console.log(s); if(!(--s)){clearInterval(id); console.log("done");}},1000);JavaScript Program to Validate An Email Address
Basic email validation.
const re=/^[^\s@]+@[^\s@]+\.[^\s@]+$/; console.log(re.test("user@example.com"));JavaScript Program to Pass Parameter to a setTimeout() Function
Pass args to setTimeout.
function show(msg){console.log(msg);} setTimeout(show,500,"Hello");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.