Working with Data
Master data handling in JavaScript. Learn essential string and array methods, map(), filter(), reduce(), the Date and Math objects, and how to work with numbers and booleans.
String Methods
Strings in JavaScript come with many built-in methods for working with text.
Common Methods:
let str = "JavaScript is awesome";
| Method | Description | Example | Result |
|---|---|---|---|
length | Returns the number of characters | str.length | 22 |
toUpperCase() | Converts to uppercase | str.toUpperCase() | "JAVASCRIPT IS AWESOME" |
toLowerCase() | Converts to lowercase | str.toLowerCase() | "javascript is awesome" |
includes() | Checks if substring exists | str.includes("is") | true |
indexOf() | Finds index of substring | str.indexOf("Script") | 4 |
slice() | Extracts part of the string | str.slice(0, 10) | "JavaScript" |
replace() | Replaces part of a string | str.replace("awesome", "cool") | "JavaScript is cool" |
split() | Splits into array | str.split(" ") | ["JavaScript", "is", ...] |
Array Methods
Arrays also come with helpful methods for managing and manipulating data.
let numbers = [10, 20, 30, 40];
| Method | Description | Example | Result |
|---|---|---|---|
push() | Add to end | numbers.push(50) | [10, 20, 30, 40, 50] |
pop() | Remove from end | numbers.pop() | Removes 50 |
shift() | Remove from start | numbers.shift() | Removes 10 |
unshift() | Add to start | numbers.unshift(5) | [5, 20, 30, 40] |
includes() | Check for item | numbers.includes(20) | true |
indexOf() | Get index of item | numbers.indexOf(30) | 2 |
join() | Convert to string | numbers.join("-") | "20-30-40" |
slice() | Extract a portion | numbers.slice(1, 3) | [20, 30] |
splice() | Insert/remove at position | numbers.splice(1, 0, 25) | [20, 25, 30, 40] |
map(), filter(), reduce()
These are powerful array transformation methods used frequently in modern JavaScript.
map() — Transform each item
let prices = [100, 200, 300];
let withTax = prices.map(p => p * 1.1);
console.log(withTax); // [110, 220, 330]filter() — Return items that match a condition
let scores = [85, 42, 77, 95];
let passed = scores.filter(score => score >= 60);
console.log(passed); // [85, 77, 95]reduce() — Reduce array to a single value
let nums = [10, 20, 30];
let total = nums.reduce((sum, num) => sum + num, 0);
console.log(total); // 60Date Object
The Date object allows you to work with dates and times.
let now = new Date();
console.log(now); // Current date and time
let birthday = new Date("2000-01-01");Useful Date Methods:
| Method | Description |
|---|---|
getFullYear() | Gets the 4-digit year |
getMonth() | Month (0–11) |
getDate() | Day of the month (1–31) |
getDay() | Day of the week (0–6) |
getHours() | Hour of the day |
toDateString() | Human-readable date |
Example:
console.log(birthday.getFullYear()); // 2000
console.log(now.toDateString()); // e.g., "Tue Jul 02 2025"
Math Object
The Math object provides built-in math functions and constants.
let num = 7.5;
Common Math Methods:
| Method | Description | Example |
|---|---|---|
Math.round() | Round to nearest integer | Math.round(num) // 8 |
Math.floor() | Round down | Math.floor(num) // 7 |
Math.ceil() | Round up | Math.ceil(num) // 8 |
Math.max() | Highest of values | Math.max(3, 7, 2) // 7 |
Math.min() | Lowest of values | Math.min(3, 7, 2) // 2 |
Math.random() | Random number (0 to <1) | Math.random() |
Math.sqrt() | Square root | Math.sqrt(16) // 4 |
Math.pow() | Exponentiation | Math.pow(2, 3) // 8 |
Number and Boolean Handling
Number Conversion:
let str = "42";
let num = Number(str); // 42
parseInt("50.99"); // 50
parseFloat("50.99"); // 50.99Check for NaN or Finite:
isNaN("abc"); // true
isFinite(100); // trueBoolean Conversion:
- Truthy: non-zero numbers, strings, arrays, objects
- Falsy:
0,"",null,undefined,NaN,false
Boolean(0); // false
Boolean("hello"); // true
Boolean([]); // trueSummary
- Use string methods like
slice(),replace(), andtoUpperCase()to work with text. - Use array methods like
push(),splice(),map(),filter(), andreduce()to manage lists. - The Date object lets you create and manipulate dates.
- The Math object gives you tools for calculations and random numbers.
- Understand how to convert and validate numbers and booleans for safe, predictable code.
Was this helpful?
Thanks for your feedback!