Strings, arrays, and objects
ECMAScript 2026
A string holds text. An array holds a sequence of values. An object groups related values by name. Start by reading one value from each kind.
On this page
Read and clean a string
A string has a length and methods, which are operations you call on a value. trim() returns a new string with whitespace removed from both ends. toLowerCase() returns a lowercase version. The original string stays as it was.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const raw = " Pen ";
console.log(raw.trim().toLowerCase());
console.log(raw.length);
What to expect It prints pen, then 7 for the original string’s length.
Replace spaces in a string
replace() returns a new string with matching text replaced. A pattern such as /\s+/g matches every run of one or more whitespace characters. The regular expression lesson explains that syntax in more detail.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const title = " Blue Pen ";
const slug = title.trim().toLowerCase().replace(/\s+/g, "-");
console.log(slug);
What to expect It prints blue-pen with one hyphen between the words.
Read and add items in an array
Write array values inside square brackets. The index is a position number that starts at zero. array[0] reads the first item. push() adds to the end, and length gives the number of items.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const products = ["Pen", "Notebook"];
console.log(products[0]);
products.push("Pencil");
console.log(products.length);
What to expect The results are Pen and 3. Try index 2 after pushing and you get Pencil.
Read named properties in an object
An object literal uses braces. Each property has a name and a value separated by a colon. Use a dot to read a known property name, or brackets to read a name stored in a variable. With const, you can still update object properties; the name must keep pointing to the same object.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const product = { name: "Pen", price: 2500 };
const key = "price";
console.log(product.name, product[key]);
product.price = 3000;
console.log(product.price);
What to expect It prints Pen and 2500, then the updated price 3000.
Extract named values with destructuring
Object destructuring copies selected property values into variables. In const { name, price } = product, both variable names match properties of product. You can also use a colon inside the pattern to assign a different variable name.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const product = { name: "Pen", price: 2500 };
const { name, price: unitPrice } = product;
console.log(name, unitPrice);
What to expect It prints Pen and 2500. The original object still uses the property named price.
Filter, transform, and total an array
filter keeps items that pass a test. map builds an array of transformed items. reduce combines them into one value, starting here at 0. The => syntax defines a short function that is called for each item.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const orders = [{ price: 2500, paid: true }, { price: 3000, paid: false }];
const paidPrices = orders.filter(order => order.paid).map(order => order.price);
console.log(paidPrices);
console.log(paidPrices.reduce((sum, price) => sum + price, 0));
What to expect It prints an array containing 2500, then the total 2500.
Understand what spread copies
Spread syntax uses three dots to copy entries from an array or properties from an object. It creates a shallow copy: nested objects are still shared. Copy the nested level too if you plan to change it independently.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const original = { product: { price: 2500 } };
const copy = { ...original, product: { ...original.product } };
copy.product.price = 3000;
console.log(original.product.price, copy.product.price);
What to expect It prints 2500 and 3000. Remove the nested copy and the original price changes as well.