Real JavaScript Objects
Build a character card. Objects store everything about one thing — its name, power, and score — so your hero travels through the code as one neat package.
What is an object?
An object is a character card — everything about one thing, stored together. Instead of three loose variables, the robot's name, power, and score travel as one neat package.
-
1
Build the card:
let robot = { ... };Curly braces
{ }make an object. Inside, each line is a property: a label, a colon, and a value —name: "Bolt". Properties are like the rows on a trading card. -
2
Read with the dot:
robot.nameThe dot reaches into the object and pulls out one property.
robot.nameis "Bolt",robot.scoreis 10. Object dot property — you'll type that thousands of times as a coder. -
3
Why coders use them
A player, an enemy, a level — each is one object carrying all its own facts. Pass
robotaround and everything about Bolt goes along for the ride. Try adding a fourth property likespeed: 99and saying it!
Store named details.
let robot = {
name: "Bolt",
power: "star scanner",
score: 10
};
say(robot.name);
say(robot.power);
say(robot.score);
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You used JavaScript to keep several named details together in one object.
Open Object Builder