Skip to content

JavaScript Objects and JSON: Properties, Methods, Arrays of Objects, stringify and parse

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

An invoice line is not just a price. It has a description, a quantity, a unit price and a VAT rate, and they belong together. Keeping them in four separate variables, or in an array where you must remember that position 2 is the price, quickly becomes a mess. Objects solve that by storing values under names: item.price, item.qty. Arrays of objects then describe whole lists, such as every line of an invoice or every branch of a bank. JSON is the text format that carries this kind of data between programs, and nearly every web API you will ever call sends and receives it.

Quick answer

Create an object with curly braces and name: value pairs: const item = { name: "Laptop", price: 3200, qty: 1 };. Read a property with a dot, item.price, or with brackets and a string, item["price"], which also works when the name is in a variable. Assign to add or change a property, item.qty = 2;. A function stored in an object is a method. Lists of records are arrays of objects, looped with for...of. JSON.stringify(item) turns an object into JSON text, and JSON.parse(text) turns JSON text back into an object.

30 min read and practice · Content checked on 6 November 2026

In this lesson you will

  • Create objects, and read, add, change and delete their properties
  • Choose between dot and bracket notation, and write simple methods
  • Work with arrays of objects, Object.entries, destructuring and ?.
  • Convert data to and from JSON, and handle invalid JSON safely

Objects: values with names

An object literal is a pair of curly braces holding properties, each a name, a colon and a value, separated by commas: const branch = { city: "Muscat", open: true, staff: 12 };. Values can be any type, including arrays and other objects. Property names are usually written without quotes; you need quotes only when a name has spaces or dashes, such as "opening-hours", which is a good reason to avoid such names.

Dot notation, branch.city, is the normal way to read a property. Bracket notation, branch["city"], takes a string, so it works when the property name is stored in a variable: const field = "staff"; branch[field]. Reading a property that does not exist gives undefined, just like a missing array index. You add or change a property by assigning to it, branch.phone = "+968 2400 0000";, and remove it with delete branch.phone;. As with arrays, an object in a const can still change inside; const only stops you replacing the whole object.

Try it: Reading and changing a branch record
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Bracket notation reads the property whose name is in field. After delete, the missing property reads as undefined, and the in operator checks whether a property exists.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between a JavaScript object and JSON?

An object is a live value in your program that can hold functions and change at any time. JSON is a piece of text that describes data in a strict format, with double quotes everywhere and no functions. You use JSON.stringify to turn an object into JSON when you save or send it, and JSON.parse to turn JSON into an object when you receive it.

When should I use an object instead of an array?

Use an array for a list of similar things in order, such as prices or invoice lines. Use an object for one thing with named parts, such as a single invoice line with its name, price and quantity. Most real data combines them: an array of objects.

How do I copy an object in JavaScript?

const copy = { ...item }; makes a shallow copy: the top level is new, but nested objects and arrays are still shared. For a full deep copy of plain data, modern browsers have structuredClone(item). Writing const copy = item; does not copy anything; both names point to the same object.

Why do I get "Unexpected token" from JSON.parse?

The text is not valid JSON. Common causes are single quotes, property names without quotes, a trailing comma, a comment, or an HTML error page returned by a server instead of JSON. Paste the text into a JSON formatter to see the exact position of the problem.

Can JSON hold Arabic text?

Yes. JSON is Unicode text, so Arabic, emoji and any other script are stored as they are. JSON.stringify({ city: "دبي" }) keeps the Arabic letters, and JSON.parse reads them back unchanged. Save JSON files as UTF-8 so other programs read them correctly.

Sources

Facts in this lesson were checked against these official pages on 6 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.