Last updated
Example: Array Methods
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Filter even numbers
numbers.filter(n => n % 2 === 0)
// → [2, 4, 6, 8, 10]
// Map to squares
numbers.map(n => n ** 2)
// → [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
// Reduce to sum
numbers.reduce((sum, n) => sum + n, 0)
// → 55
// Chain operations
numbers
.filter(n => n % 2 === 0)
.map(n => n * 10)
.reduce((sum, n) => sum + n, 0)
// → 300
Example: Object Destructuring and Spread
const user = { name: 'Alice', age: 30, city: 'New York' };
// Destructuring
const { name, age } = user;
console.log(name, age);
// → Alice 30
// Spread to merge objects
const updatedUser = { ...user, age: 31, email: 'alice@example.com' };
console.log(updatedUser);
// → { name: 'Alice', age: 31, city: 'New York', email: 'alice@example.com' }
// Rest parameters
const { name: userName, ...rest } = user;
console.log(rest);
// → { age: 30, city: 'New York' }
Example: Promises and Async/Await
// Promise chain
Promise.resolve(42)
.then(n => n * 2)
.then(n => n + 10)
.then(console.log);
// → 94
// Async function
async function fetchData() {
const result = await Promise.resolve({ status: 'ok', data: [1, 2, 3] });
return result.data.map(n => n * 2);
}
fetchData().then(console.log);
// → [2, 4, 6]
// Promise.all
Promise.all([
Promise.resolve('first'),
Promise.resolve('second'),
Promise.resolve('third')
]).then(console.log);
// → ["first", "second", "third"]
Example: Closures
// Counter using closure
function makeCounter(start = 0) {
let count = start;
return {
increment: () => ++count,
decrement: () => --count,
value: () => count
};
}
const counter = makeCounter(10);
counter.increment(); // → 11
counter.increment(); // → 12
counter.decrement(); // → 11
counter.value(); // → 11
Example: Prototype and Classes
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound.`;
}
}
class Dog extends Animal {
speak() {
return `${this.name} barks.`;
}
}
const d = new Dog('Rex');
d.speak(); // → "Rex barks."
d instanceof Dog; // → true
d instanceof Animal; // → true
Object.getPrototypeOf(d) === Dog.prototype; // → true
Example: console.table Output
const users = [
{ name: 'Alice', age: 30, role: 'admin' },
{ name: 'Bob', age: 25, role: 'user' },
{ name: 'Carol', age: 35, role: 'user' }
];
console.table(users);
// Output:
// ┌─────────┬─────────┬─────┬─────────┐
// │ (index) │ name │ age │ role │
// ├─────────┼─────────┼─────┼─────────┤
// │ 0 │ 'Alice' │ 30 │ 'admin' │
// │ 1 │ 'Bob' │ 25 │ 'user' │
// │ 2 │ 'Carol' │ 35 │ 'user' │
// └─────────┴─────────┴─────┴─────────┘
Example: Error Handling
// Try/catch
try {
JSON.parse('{ invalid json }');
} catch (e) {
console.error('Parse error:', e.message);
}
// → Parse error: Unexpected token i in JSON at position 2
// Custom error
class ValidationError extends Error {
constructor(field, message) {
super(message);
this.name = 'ValidationError';
this.field = field;
}
}
try {
throw new ValidationError('email', 'Invalid email format');
} catch (e) {
console.log(e.name, e.field, e.message);
}
// → ValidationError email Invalid email format
Example: Generator Functions
function* range(start, end, step = 1) {
for (let i = start; i < end; i += step) {
yield i;
}
}
[...range(0, 10, 2)]
// → [0, 2, 4, 6, 8]
function* fibonacci() {
let [a, b] = [0, 1];
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const fib = fibonacci();
Array.from({ length: 8 }, () => fib.next().value)
// → [0, 1, 1, 2, 3, 5, 8, 13]
Example: Regular Expressions
const text = 'Contact us at support@example.com or sales@company.org';
// Find all emails
const emails = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g);
console.log(emails);
// → ["support@example.com", "sales@company.org"]
// Replace with named groups
'2024-01-15'.replace(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
'$<day>/$<month>/$<year>'
);
// → "15/01/2024"
Type any JavaScript directly into the emulator and press Run to see the output immediately. No installation, no setup — just open the tool and start coding.
Example: Basic Expressions
// Arithmetic
2 + 2 // → 4
10 / 3 // → 3.3333333333333335
2 ** 10 // → 1024
Math.sqrt(144) // → 12
// String operations
'Hello'.toUpperCase() // → "HELLO"
' trim me '.trim() // → "trim me"
'a,b,c'.split(',') // → ["a", "b", "c"]
[1, 2, 3].join(' - ') // → "1 - 2 - 3"