Master JavaScript through hands-on coding challenges. Write real code, get instant feedback, and build practical web development skills.
Master powerful array methods and string operations used in real-world JavaScript development.
map(), filter(), and reduce(). These functional programming methods are essential for modern JavaScript development.const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];map() to transform each element - The map() method creates a NEW array by calling a function on every element. It does NOT modify the original array. Create a new array with each number doubled.const doubled = numbers.map(num => num * 2);
// Result: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
// map() takes a callback: (element, index, array) => newValuefilter() to select elements - The filter() method creates a NEW array with elements that pass a test (return true). Filter the numbers to get only even numbers.const evens = numbers.filter(num => num % 2 === 0);
// Result: [2, 4, 6, 8, 10]
// filter() keeps elements where callback returns truereduce() to calculate a single value - The reduce() method reduces an array to a single value by executing a reducer function. Calculate the sum of all numbers.const sum = numbers.reduce((acc, num) => acc + num, 0);
// Result: 55
// reduce(callback, initialValue)
// callback: (accumulator, currentValue, index, array) => newAccumulatorconst result = numbers
.filter(n => n % 2 === 0) // [2, 4, 6, 8, 10]
.map(n => n * 2) // [4, 8, 12, 16, 20]
.reduce((a, b) => a + b, 0); // 60map() always returns an array of the SAME lengthfilter() returns an array of the SAME or SMALLER lengthreduce() can return any type (number, string, object, array)reduce() to avoid errors with empty arraysreturn a value in map/filter callbacks (when using braces)map() with forEach() (forEach returns undefined)reduce()Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Doubled: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Even numbers: [2, 4, 6, 8, 10]
Sum: 55
Chained result: 60
array.map(element => transformedElement)array.filter(element => condition)array.reduce((acc, curr) => acc + curr, initialValue)n => n * 2 (implicit return)arr.filter(...).map(...).reduce(...)Review feedback below
const message = " Hello, JavaScript World! "; (note the spaces)trim() to remove whitespace - Removes whitespace from both ends of a string. Also available: trimStart() and trimEnd().const trimmed = message.trim();
// "Hello, JavaScript World!" (spaces removed)toUpperCase() and toLowerCase() - Convert case of all characters. Useful for case-insensitive comparisons.const upper = message.toUpperCase(); // " HELLO, JAVASCRIPT WORLD! "
const lower = message.toLowerCase(); // " hello, javascript world! "includes() to check for substring - Returns true if the string contains the specified substring, false otherwise. Case-sensitive!message.includes("JavaScript"); // true
message.includes("Python"); // falsesplit() to convert to array - Splits a string into an array using a delimiter. Very useful for parsing data.const words = "apple,banana,orange".split(",");
// ["apple", "banana", "orange"]
"Hello World".split(" "); // ["Hello", "World"]
"Hello".split(""); // ["H", "e", "l", "l", "o"]replace() to substitute text - Replaces first occurrence. Use replaceAll() or regex with /g flag for all occurrences."Hello World".replace("World", "JavaScript");
// "Hello JavaScript"
"aaa".replaceAll("a", "b"); // "bbb"slice(start, end) extracts a section (end not included)substring(start, end) similar to slice but handles negative indices differentlystartsWith() and endsWith() check string boundariespadStart() and padEnd() add padding to reach desired lengthincludes() is case-sensitive== instead of === for string comparisonOriginal: " Hello, JavaScript World! "
Trimmed: "Hello, JavaScript World!"
Uppercase: " HELLO, JAVASCRIPT WORLD! "
Lowercase: " hello, javascript world! "
Includes 'JavaScript': true
Words array: ["Hello,", "JavaScript", "World!"]
Replaced: "Hello, Python World!"
str.trim() removes whitespace from both endsstr.toUpperCase() / str.toLowerCase()str.includes("text") returns true/falsestr.split(" ") splits by spacestr.replace("old", "new")str.length (property, not method)Review feedback below
name, age, and city variables.${expression}. Much cleaner than string concatenation!const name = "Alice";
const age = 25;
const greeting = `Hello, my name is ${name} and I am ${age} years old.`;
// "Hello, my name is Alice and I am 25 years old."
// You can include expressions:
const message = `Next year I'll be ${age + 1} years old.`;
// Multi-line strings (no \n needed):
const multiLine = `Line 1
Line 2
Line 3`;const colors = ["red", "green", "blue"];
const [first, second, third] = colors;
// first = "red", second = "green", third = "blue"
// Skip elements with empty slots:
const [primary, , tertiary] = colors;
// primary = "red", tertiary = "blue"
// Rest operator to collect remaining:
const [head, ...tail] = colors;
// head = "red", tail = ["green", "blue"]const person = { name: "Bob", age: 30, city: "NYC" };
const { name, age, city } = person;
// name = "Bob", age = 30, city = "NYC"
// Rename during destructuring:
const { name: personName, age: personAge } = person;
// personName = "Bob", personAge = 30
// Default values:
const { name, country = "USA" } = person;
// country = "USA" (default, since it doesn't exist)// Instead of: function greet(person) { return person.name; }
function greet({ name, age }) {
return `${name} is ${age} years old`;
}
greet({ name: "Alice", age: 25 }); // "Alice is 25 years old"const { address: { city } } = person;[a, b] = [b, a];for (const { name } of people) { }Template: Hello, Alice! You are 25 years old and live in Seattle.
Array destructuring: red, green, blue
Object destructuring: Bob, 30, NYC
Function with destructuring: Charlie is 28 years old
`Hello, ${name}!` (use backticks!)const [a, b, c] = array;const { prop1, prop2 } = object;const { name: newName } = obj;({ name, age }) => `${name}: ${age}`const { x = 10 } = obj;Review feedback below