Skip to content

Commit 0f3c96b

Browse files
Dario-DCjdwilkin4
andauthored
chore(curriculum): add working with objects transcripts (freeCodeCamp#58708)
Co-authored-by: Jessica Wilkins <[email protected]>
1 parent 9491cbf commit 0f3c96b

File tree

11 files changed

+678
-11
lines changed

11 files changed

+678
-11
lines changed

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/67329f737126b75bcb949e13.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,106 @@ dashedName: what-is-an-object-in-javascript-and-how-can-you-access-properties-fr
88

99
# --description--
1010

11-
Watch the video lecture and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What is a JavaScript object, and how can you access properties from an object?
16+
17+
In JavaScript, an object is a fundamental data structure that allows you to store and organize related data and functionality.
18+
19+
You can think of an object as a container that holds various pieces of information, much like a filing cabinet holds different folders and documents.
20+
21+
These pieces of information are called properties, and they consist of a name (or key) and a value.
22+
23+
```js
24+
const exampleObject = {
25+
propertyName: value;
26+
}
27+
```
28+
29+
Objects are incredibly versatile and form the backbone of JavaScript. In fact, almost everything in JavaScript is an object or can be treated as one. This includes arrays, functions, and even primitive data types like strings and numbers when used in certain ways.
30+
31+
This object-centric nature of JavaScript is one of the reasons it's such a flexible and powerful language. Let's look at how you can create an object:
32+
33+
```js
34+
const person = {
35+
name: "Alice",
36+
age: 30,
37+
city: "New York"
38+
};
39+
```
40+
41+
In this example, we've created an object called `person` with three properties: `name`, `age`, and `city`. Each property has a name and a value, separated by a colon.
42+
43+
Now, let's explore how you can access these properties. There are two main ways to access object properties in JavaScript: dot notation and bracket notation.
44+
45+
Dot notation is the most common and straightforward way to access object properties. Here is the basic syntax for dot notation:
46+
47+
```js
48+
objectName.propertyName
49+
```
50+
51+
Here's how you would use dot notation with our `person` object:
52+
53+
```js
54+
const person = {
55+
name: "Alice",
56+
age: 30,
57+
city: "New York"
58+
};
59+
60+
console.log(person.name); // Alice
61+
console.log(person.age); // 30
62+
```
63+
64+
Dot notation is concise and easy to read, making it the preferred choice when you know the exact name of the property you want to access and that name is a valid JavaScript identifier (meaning it doesn't start with a number and doesn't contain special characters or spaces).
65+
66+
Bracket notation, on the other hand, allows you to access object properties using a string inside square brackets. Here's how you would use bracket notation:
67+
68+
```js
69+
const person = {
70+
name: "Alice",
71+
age: 30,
72+
city: "New York"
73+
};
74+
75+
console.log(person["name"]); // Alice
76+
console.log(person["age"]); // 30
77+
```
78+
79+
Bracket notation is more flexible than dot notation because it allows you to use property names that aren't valid JavaScript identifiers. For example, if you had a property name with spaces or that starts with a number, you'd need to use bracket notation:
80+
81+
```js
82+
const oddObject = {
83+
"1stProperty": "Hello",
84+
"property with spaces": "World"
85+
};
86+
87+
console.log(oddObject["1stProperty"]); // Hello
88+
console.log(oddObject["property with spaces"]); // World
89+
```
90+
91+
Another advantage of bracket notation is that it allows you to use variables to access properties dynamically:
92+
93+
```js
94+
const person = {
95+
name: "Alice",
96+
age: 30,
97+
city: "Wonderland"
98+
};
99+
100+
let propertyName = "city";
101+
console.log(person[propertyName]); // Wonderland
102+
```
103+
104+
This flexibility makes bracket notation particularly useful when you don't know the exact property name at the time you're writing the code, or when you're working with property names that come from user input or some other dynamic source.
105+
106+
It's worth noting that objects in JavaScript are incredibly powerful and versatile. They can contain not just simple values like strings and numbers, but also arrays, or other objects.
107+
108+
Understanding objects and how to work with them is crucial in JavaScript because they're used extensively throughout the language and in many JavaScript libraries and frameworks.
109+
110+
As you continue to learn and work with JavaScript, you'll find that mastering objects opens up a world of possibilities for creating complex and powerful applications.
12111

13112
# --questions--
14113

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/6732b721eb98f224868b44a6.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,49 @@ dashedName: how-can-you-remove-properties-from-an-object
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
How can you remove properties from an object?
16+
17+
There are several ways to remove properties from an object, with the `delete` operator being the most straightforward and commonly used method.
18+
19+
When you use `delete`, it removes the selected property from the object. Here's an example of how to use the `delete` operator:
20+
21+
```js
22+
const person = {
23+
name: "Alice",
24+
age: 30,
25+
job: "Engineer"
26+
};
27+
28+
delete person.job;
29+
30+
console.log(person.job); // undefined
31+
```
32+
33+
In this example, we start with a `person` object that has three properties: `name`, `age`, and `job`. Then, we use the `delete` operator to remove the `job` property. After the deletion, the `person` object no longer has the `job` property.
34+
35+
Another way to remove properties is by using destructuring assignment with rest parameters. This approach doesn't actually delete the property, but it creates a new object without the specified properties:
36+
37+
```js
38+
const person = {
39+
name: "Bob",
40+
age: 25,
41+
job: "Designer",
42+
city: "New York"
43+
};
44+
45+
const { job, city, ...remainingProperties } = person;
46+
47+
// { name: "Bob", age: 25 }
48+
console.log(remainingProperties);
49+
```
50+
51+
In this example, we use destructuring to extract `job` and `city` from the `person` object, and collect the remaining properties into a new object called `remainingProperties`. This creates a new object without the `job` and `city` properties.
52+
53+
Understanding how to remove properties from objects is an important skill in JavaScript programming. It allows you to manipulate objects dynamically, and clean up unnecessary data.
1254

1355
# --questions--
1456

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/6732b72961f94324bd6390de.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,58 @@ dashedName: how-to-check-if-an-object-has-a-property
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
How to check if an object has a property?
16+
17+
In JavaScript, there are several ways to check if an object has a specific property. Understanding these methods is important for working effectively with objects, especially when you're dealing with data from external sources or when you need to ensure certain properties exist before using them.
18+
19+
We'll explore three common approaches: the `hasOwnProperty()` method, the `in` operator, and checking against `undefined`.
20+
21+
Let's start with the `hasOwnProperty()` method. This method returns a boolean indicating whether the object has the specified property as its own property. Here's an example:
22+
23+
```js
24+
const person = {
25+
name: "Alice",
26+
age: 30
27+
};
28+
29+
console.log(person.hasOwnProperty("name")); // true
30+
console.log(person.hasOwnProperty("job")); // false
31+
```
32+
33+
In this example, we have an object called `person` with two properties: `name` and `age`. To check if `name` is a property in the `person` object, we use the `hasOwnProperty()` method. Since `name` is a property, it will return `true`. But when we use the `hasOwnProperty()` to check if `job` is a property, it will return `false` because it does not exist in the object.
34+
35+
Another way to check for the existence of a property in an object is to use the `in` operator. Like `hasOwnProperty()`, the `in` operator will return `true` if the property exists on the object. Here's how you can use it:
36+
37+
```js
38+
const person = {
39+
name: "Bob",
40+
age: 25
41+
};
42+
console.log("name" in person); // true
43+
```
44+
45+
In this example, `"name" in person` returns `true` because `name` is a property of `person`.
46+
47+
The third method involves checking if a property is `undefined`. This approach can be useful, but it has some limitations. Here's an example:
48+
49+
```js
50+
const car = {
51+
brand: "Toyota",
52+
model: "Corolla",
53+
year: 2020
54+
};
55+
56+
console.log(car.brand !== undefined); // true
57+
console.log(car.color !== undefined); // false
58+
```
59+
60+
In this code, we check if `car.brand` and `car.color` are not `undefined`. This works because accessing a non-existent property on an object returns `undefined`. However, this method can give false negatives if a property explicitly has the value `undefined`.
61+
62+
In practice, the choice between these methods often depends on the specific requirements of your code. Understanding the differences between them will help you make the right choice in different scenarios.
1263

1364
# --questions--
1465

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/6732b73509f71f24ef05e86e.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,66 @@ dashedName: how-do-you-work-with-accessing-properties-from-nested-objects-and-ar
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
How do you work with accessing properties from nested objects and arrays in objects?
16+
17+
When working with JavaScript, you'll often encounter complex data structures that involve nested objects and arrays within objects. These structures can represent rich, hierarchical data, but they also require a clear understanding of how to access and manipulate the data within them. Let's explore how to navigate these nested structures effectively.
18+
19+
Accessing properties from nested objects involves using the dot notation or bracket notation, much like accessing properties from simple objects. However, you'll need to chain these accessors to drill down into the nested structure.
20+
21+
For example, let's consider a nested object representing a person with contact information:
22+
23+
```js
24+
const person = {
25+
name: "Alice",
26+
age: 30,
27+
contact: {
28+
29+
phone: {
30+
home: "123-456-7890",
31+
work: "098-765-4321"
32+
}
33+
}
34+
};
35+
```
36+
37+
To access `Alice`'s work phone number, you would chain the property accessors like this:
38+
39+
```js
40+
console.log(person.contact.phone.work); // "098-765-4321"
41+
```
42+
43+
You can also use bracket notation, which is particularly useful when property names include spaces or special characters, or when you're using variables to access properties:
44+
45+
```js
46+
console.log(person['contact']['phone']['work']); // "098-765-4321"
47+
```
48+
49+
Now, let’s take a look at how we can access data where one of the object properties has the value of an array. Here is a modified `person` object that includes an array of addresses:
50+
51+
```js
52+
const person = {
53+
name: "Alice",
54+
age: 30,
55+
addresses: [
56+
{ type: "home", street: "123 Main St", city: "Anytown" },
57+
{ type: "work", street: "456 Market St", city: "Workville" }
58+
]
59+
};
60+
```
61+
62+
Here is an example of how to access `Alice`'s work address city:
63+
64+
```js
65+
console.log(person.addresses[1].city); // "Workville"
66+
```
67+
68+
In this example, `person.addresses` refers to the array of addresses. To access the second address in that array, we use bracket notation and index `1`. Then, we use dot notation to access the `city` from that address object.
69+
70+
Understanding how to access properties in nested objects and arrays is essential when working with complex data structures. In future workshops and labs, you will have the opportunity to practice working with these types of data structures.
1271

1372
# --questions--
1473

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/6732b73d25cc01251b778043.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,54 @@ dashedName: what-is-the-difference-between-primitive-and-non-primitive-data-type
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What is the difference between primitive and non-primitive data types?
16+
17+
In JavaScript, understanding the difference between primitive and non-primitive data types is important for writing efficient and bug-free code.
18+
19+
These two categories of data types behave differently in terms of how they are stored in memory and how they are manipulated in your programs.
20+
21+
Primitive data types are the simplest form of data in JavaScript. They include numbers, strings, booleans, `null`, `undefined`, and symbols. These types are called "primitive" because they represent single values and are not objects.
22+
23+
When you work with primitive data types, you're dealing directly with their values. For example, when you create a variable with a primitive value, that value is stored directly in the variable.
24+
25+
Primitive values are immutable, which means once they are created, their value cannot be changed. However, you can reassign a new value to the variable. Here's an example of working with primitive data types:
26+
27+
```js
28+
let num1 = 5;
29+
let num2 = num1;
30+
num1 = 10;
31+
32+
console.log(num2); // 5
33+
```
34+
35+
In this example, we are assigning a primitive value (`5`) from `num1` to `num2`. This creates an independent copy of the value. As a result, any changes made to the original variable (`num1`) do not affect the copy (`num2`).
36+
37+
Non-primitive data types, on the other hand, are more complex. In JavaScript, these are objects, which include regular objects, arrays, and functions. Unlike primitives, non-primitive types can hold multiple values as properties or elements.
38+
39+
When you create a variable with a non-primitive value, what's stored in the variable is actually a reference to the location in memory where the object is stored, not the object itself. This leads to some important differences in behavior. Here's an example with non-primitive types:
40+
41+
```js
42+
const originalPerson = { name: "John", age: 30 };
43+
const copiedPerson = originalPerson;
44+
45+
originalPerson.age = 31;
46+
47+
console.log(copiedPerson.age); // 31
48+
```
49+
50+
In this example we have an object called `originalPerson` with two properties of `name` and `age`. We then assign the `originalPerson` object to a variable called `copiedPerson`.
51+
52+
Then we update the `age` value for the `originalPerson` object. When we log the `age` property of `copiedPerson` object it shows the updated value.
53+
54+
But why is that happening? This occurs because both `originalPerson` and `copiedPerson` are referencing the same object in memory.
55+
56+
In JavaScript, when you assign an object to another variable, you're copying the reference to the object, not the object itself. This is known as shallow copying by reference. As a result, any changes made to the object through one reference are reflected in all references to that object.
57+
58+
As you continue to work with JavaScript, you'll encounter many situations where understanding the difference between primitive and non-primitive types is important. It's a fundamental concept that underlies many aspects of the language and is key to writing efficient and correct code.
1259

1360
# --questions--
1461

curriculum/challenges/english/25-front-end-development/lecture-working-with-objects/6732b749b8aad125523dcda5.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,46 @@ dashedName: what-is-the-difference-between-functions-and-object-methods
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What is the difference between functions and methods?
16+
17+
In JavaScript, functions and object methods are both ways to encapsulate reusable code, but they have some key differences in how they are defined, used, and the context in which they operate. Understanding these differences is crucial for writing effective and organized JavaScript code.
18+
19+
As you learned in earlier modules, functions are reusable blocks of code that perform a specific task:
20+
21+
```js
22+
function greet(name) {
23+
return "Hello, " + name + "!";
24+
}
25+
console.log(greet("Alice")); // "Hello, Alice!"
26+
```
27+
28+
Object methods, on the other hand, are functions that are associated with an object. They are defined as properties of an object and can access and manipulate the object's data. Here's an example of an object with a method:
29+
30+
```js
31+
const person = {
32+
name: "Bob",
33+
age: 30,
34+
sayHello: function() {
35+
return "Hello, my name is " + this.name;
36+
}
37+
};
38+
39+
console.log(person.sayHello()); // "Hello, my name is Bob"
40+
```
41+
42+
In this example, `sayHello` is a method of the `person` object. The `this` keyword allows the `sayHello` method to access the properties of the object named `person`. You will learn more about the `this` keyword in future lectures.
43+
44+
A difference between functions and methods is how they are invoked. Functions are called by their name, while methods are called using dot notation on the object they belong to. For example, we call the `greet` function as `greet("Alice")`, but we call the `sayHello` method as `person.sayHello()`.
45+
46+
Another important difference is the context in which they operate. Regular functions have their own scope, but they don't have a built-in reference to any particular object. Methods, however, are bound to their object and can access its properties and other methods using the `this` keyword.
47+
48+
A key point to note is that, methods help in organizing code into logical objects, while functions are used for more general, reusable code.
49+
50+
Understanding the difference between functions and object methods is important as you progress in your JavaScript journey. While they may seem similar at first, recognizing when to use each will help you write more organized, efficient, and better code.
1251

1352
# --questions--
1453

0 commit comments

Comments
 (0)