JavaScript Objects: Code-Along
Follow the instructions to create a small Node console app that creates and manipulates a JavaScript object.
Starter App
- Create a new Node.js Repl project
- Name it "Car Objects"
- Create a new file named object.js in the current directory
- Open the file, and enter the following text:
console.log('Welcome to the car app!'); - Create a new file named .replit and add
run = "node object.js"to the file - Click the "Run" button, and "Welcome to the car app!" should appear in the console!
Defining the Object
- On the next line in the object.js file, create a new object variable named
myCar- Use
let, the object name,=, and{}
- Use
- Add a
colorproperty with a value of'tan'- Use the property name,
:, the value, and,
- Use the property name,
- Add additional properties with the following values:
year- 1987make- Oldsmobilemodel- Cutlass Ciera
Adding the Owner
- In the
myCarobject definition, add another property:owner - The value of the
ownerproperty will be another object! Start by setting the value to an empty object ({}) - Within the
ownerobject, add properties with the following values:firstName- JerrylastName- Lundegaardcity- Minneapolisstate- MN
Adding the Features
- In the
myCarobject definition, add another property:features - The value of the
featuresproperty will be an array! Start by setting the value to an empty array ([]) - Within the
featuresarray, add the following values:- "Heated seats"
- "Airbags"
- "Power steering"
The owner object should look something like this:
let myCar = {
color: 'tan',
year: 1987,
make: 'Oldsmobile',
model: 'Cutlass Ciera',
owner: {
firstName: 'Jerry',
lastName: 'Lundegaard',
city: 'Minneapolis',
state: 'MN'
},
features: [
"Heated seats",
"Airbags",
"Power steering"
]
}
Accessing the Object Properties
Now that the object has been created, it is possible to access the data to read or change it.
Displaying the Current Information
- Use a
console.logstatement to print out a message with the color of the car- Start the message with "The car is "
- Use dot notation to access the
colorproperty ofmyCar
- Use another
console.logstatement to print out a message with the first name of the owner- Start the message with "The car is owned by "
- Use dot notation to access the
ownerproperty ofmyCar - Then, use dot notation again to access the
firstNameproperty ofowner(this can be chained)
console.log('The car is ' + myCar.color);
console.log('The car is owned by ' + myCar.owner.firstName);
Updating the Color
- On the next line, use dot notation to set the
colorproperty ofmyCarto red - Use a
console.logto show a message saying "Jerry painted the car. The car is now " ... and access the new color
myCar.color = 'red'
console.log('Jerry painted the car. The car is now ' + myCar.color);
Updating the Owner's Residence
- On the next line, use dot notation to set the
cityproperty of theownerofmyCarto "Fargo"- Use a chained dot notation to access the
cityfrom themyCarobject
- Use a chained dot notation to access the
- Use dot notation again to set the
stateproperty of theownerofmyCarto "ND" - Use another
console.logto show a message saying Jerry moved. It should give his current city and state of residence
myCar.owner.city = 'Fargo';
myCar.owner.state = 'ND';
console.log('Jerry moved and now resides in ' + myCar.owner.city + ', ' + myCar.owner.state);
Adding a Feature
Currently, the car has a few features. Add another one to the array, and then print out the total number of features.
- Create a new variable named
carFeatures, and use dot notation to store thefeaturesof the car - On the next line, use
pushto add a new feature to the list: "Cruise control" - Create a new variable named
featureCount, and use.lengthto store the number of features of the car - Under that, use a
console.logto show a message with the feature count for the car
let carFeatures = myCar.features;
carFeatures.push("Cruise control");
let featureCount = carFeatures.length;
console.log('The car has ' + featureCount + ' features');
Final Code
console.log('Welcome to the car app!');
let myCar = {
color: 'tan',
year: 1987,
make: 'Oldsmobile',
model: 'Cutlass Ciera',
owner: {
firstName: 'Jerry',
lastName: 'Lundegaard',
city: 'Minneapolis',
state: 'MN'
},
features: [
"Heated seats",
"Airbags",
"Power steering"
]
};
console.log('The car is ' + myCar.color);
console.log('The car is owned by ' + myCar.owner.firstName);
myCar.color = 'red';
console.log('Jerry painted the car. The car is now ' + myCar.color);
myCar.owner.city = 'Fargo';
myCar.owner.state = 'ND';
console.log('Jerry moved and now resides in ' + myCar.owner.city + ', ' + myCar.owner.state);
let carFeatures = myCar.features;
carFeatures.push("Cruise control");
let featureCount = carFeatures.length;
console.log('The car has ' + featureCount + ' features');