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.
Console
- 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
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.
- MDN: JavaScript object basics
- MDN: Working with objects
- MDN: Object.entries()
- MDN: Optional chaining (?.)
- MDN: Working with JSON
- MDN: JSON
- MDN: try...catch
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.