JavaScript Objects Worksheet
Question 1
What makes up an object (what does an object consist of)?
An object is a structure for storing data and functionality together. An object is made up of three things; properties, methods, and prototypes. Properties are key-value pairs that store data (color: "red"); methods are functions inside of an object that performs actions (startEngine()); prototypes are objects that can inherit shared features which enable shared functionality across objects.
Question 2
What are two different types of notations that you can use when working with the properites of objects? For example, there are two types of syntax that you could use if you wanted to update a property of an object.
You can use either dot-notation or bracket-notation for object properties. Using dot-notation allows for access or change to a property, like 'person.name = "Bob"'. Bracket-notation does the same thing, like 'person["name"] = "Bob"', but each is used for different contexts. Dot-notation is used to update a property, and bracket-notation is useful for updating dynamic properties or anything that contain special characters.
Question 3
Why are objects an important data type in JavaScript?
Objects are important in JS because they help organize, manage, and store data in a structured way. Essentially, objects make coding more efficient, readable, and scalable.
Question 4
When creating an object, what character is used to separate property names from their values?
A colon is used to separate the property name from their values.
Example:
Example:
let person = {
name: "Jon Snow",
age: 30
};
Question 5
When creating an object, what character is used to separate the property/value pairs in an object?
A comma is used to separate the property or value pairs.
Example:
Example:
let person = {
name: "Jon Snow", // comma separates this from the next one
age: 30 // last pair does not need a comma
};
Question 6
What operator is used to access a property of an object?
A period-operator is used to access the property.
Example:
Example:
let car = {
color: "red",
model: "sedan"
};
console.log(car.color); // period-operator accesses the stored data for property 'color' for object of 'car'