Working with Data

ADVERTISEMENT

String Methods

Strings in JavaScript come with many built-in methods for working with text.

Common Methods:

let str = "JavaScript is awesome";
MethodDescriptionExampleResult
lengthReturns the number of charactersstr.length22
toUpperCase()Converts to uppercasestr.toUpperCase()"JAVASCRIPT IS AWESOME"
toLowerCase()Converts to lowercasestr.toLowerCase()"javascript is awesome"
includes()Checks if substring existsstr.includes("is")true
indexOf()Finds index of substringstr.indexOf("Script")4
slice()Extracts part of the stringstr.slice(0, 10)"JavaScript"
replace()Replaces part of a stringstr.replace("awesome", "cool")"JavaScript is cool"
split()Splits into arraystr.split(" ")["JavaScript", "is", ...]

Array Methods

Arrays also come with helpful methods for managing and manipulating data.

let numbers = [10, 20, 30, 40];
MethodDescriptionExampleResult
push()Add to endnumbers.push(50)[10, 20, 30, 40, 50]
pop()Remove from endnumbers.pop()Removes 50
shift()Remove from startnumbers.shift()Removes 10
unshift()Add to startnumbers.unshift(5)[5, 20, 30, 40]
includes()Check for itemnumbers.includes(20)true
indexOf()Get index of itemnumbers.indexOf(30)2
join()Convert to stringnumbers.join("-")"20-30-40"
slice()Extract a portionnumbers.slice(1, 3)[20, 30]
splice()Insert/remove at positionnumbers.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); // 60

Date 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:

MethodDescription
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:

MethodDescriptionExample
Math.round()Round to nearest integerMath.round(num) // 8
Math.floor()Round downMath.floor(num) // 7
Math.ceil()Round upMath.ceil(num) // 8
Math.max()Highest of valuesMath.max(3, 7, 2) // 7
Math.min()Lowest of valuesMath.min(3, 7, 2) // 2
Math.random()Random number (0 to <1)Math.random()
Math.sqrt()Square rootMath.sqrt(16) // 4
Math.pow()ExponentiationMath.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.99

Check for NaN or Finite:

isNaN("abc");      // true
isFinite(100);     // true

Boolean Conversion:

  • Truthy: non-zero numbers, strings, arrays, objects
  • Falsy: 0, "", null, undefined, NaN, false
Boolean(0);         // false
Boolean("hello");   // true
Boolean([]);        // true

Summary

  • Use string methods like slice(), replace(), and toUpperCase() to work with text.
  • Use array methods like push(), splice(), map(), filter(), and reduce() 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.

ADVERTISEMENT