An object as an ID card
The key favourite subject has a space, so it needs quotes when you write it and square brackets when you read it.
An object is a collection of named values called properties; you write one with curly braces, and its values can be anything, including functions (methods), with shorthand and computed keys to keep the code short.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Make an object phone with brand, model, price and a method label() that returns "<brand> <model> costs <price>". Log phone.label().
Given const key = "level" and const value = 3, build an object whose key is the value of key, using a computed key, and log it.
Write an arrow function point(x, y) that returns { x, y } using property shorthand. Log point(2, 5).
Explain it without notes
What is an object in JavaScript, and how is it different from an array?
What do property shorthand, method shorthand and computed keys do? Give an example of each.
Why does () => { ok: true } return undefined?
In what order do an object's keys come back?
Preview
Console
Nothing logged yet.Expected console output
{ name: 'Asha', age: 14, 'favourite subject': 'Maths', isMonitor: true }
Asha 14
object