Dot vs Bracket Notation | JavaScript 🔥 | Lecture 040

The Coding Classroom
7 Apr 202319:21

Summary

TLDRThe video explains how to retrieve and modify data from JavaScript objects using dot and bracket notation. It covers using variables and expressions with bracket notation to access properties dynamically. Challenges viewers to compose a sentence pulling data from an object. Also explains operator precedence for dot and bracket notation, executing left-to-right. Key concepts are accessing properties, dot vs bracket notation, computed expressions, and chaining operators.

Takeaways

  • 😀 There are two ways to retrieve/change data from objects: dot notation (obj.key) and bracket notation (obj['key'])
  • 📝 Bracket notation allows computing property names, dot does not
  • 💡 Use brackets when property names are dynamic, else use dot notation - it's cleaner
  • 🔎 The 'this' dot goes to the object and gets the property with the specified name
  • 📋 Can add new properties to objects simply by assigning values using dot/bracket notation
  • ⏩ Dot and bracket notation have high operator precedence, executed left-to-right
  • 📚 'undefined' means accessing a non-existent property; it's a falsy value
  • ✅ Can use 'undefined' check to handle invalid user requests for properties
  • 🤝 The 'length' property on arrays returns number of elements
  • 💪 Challenge is to dynamically print 'Jonas has 3 friends, best is Michael' from data

Q & A

  • What are the two ways to retrieve data from objects in JavaScript?

    -The two ways are: 1) Using the dot notation (object.property), and 2) Using bracket notation (object['property']).

  • When should you use bracket notation over dot notation?

    -Use bracket notation when you need to compute the property name dynamically, such as when getting the property name from a variable.

  • What does the 'this' keyword refer to in dot notation?

    -The 'this' keyword refers to the object itself. So 'object.property' means get the property on this object.

  • What happens if you try to access a property that does not exist on an object?

    -You get undefined when trying to access a non-existent property on an object.

  • How can you iterate over the properties of an object?

    -You can use a for-in loop to iterate over the properties of an object.

  • What is the difference between arrays and objects in JavaScript?

    -Arrays contain ordered data indexed by numbers. Objects contain unordered data indexed by key names.

  • How do you add a new property to an existing object?

    -Use either dot notation or bracket notation. For example: object.newProperty = 'value' or object['newProperty'] = 'value'.

  • What is the purpose of the length property on arrays?

    -The length property returns the number of elements in an array.

  • What data types can properties hold in a JavaScript object?

    -Properties can hold any valid JavaScript data type - strings, numbers, booleans, arrays, other objects, etc.

  • Can you nest objects and arrays inside each other in JavaScript?

    -Yes, JavaScript allows arbitrarily complex nesting of objects and arrays.

Outlines

00:00

😊 Using Dot and Bracket Notation to Access Object Properties

This paragraph explains the dot and bracket notation for accessing properties on a JavaScript object. It shows how to use dot notation (object.property) and bracket notation (object['property']) to retrieve data from an object. It also demonstrates that bracket notation allows dynamic property names computed at runtime, while dot notation requires the actual property name.

05:02

😃 When to Use Dot vs Bracket Notation

This paragraph summarizes when to use dot notation vs bracket notation. Use bracket notation when you need to compute the property name dynamically. Use dot notation in all other cases when you know the exact property name, as it looks cleaner.

10:03

😎 Interactive Example with Prompt() and Accessing Properties

This paragraph shows an interactive example using the prompt() function to get user input on which property to access. It demonstrates using bracket notation to access the chosen property dynamically. It also handles the case when the user chooses a non-existent property by checking for undefined.

15:08

😊 Adding New Properties and the this Keyword Challenge

This paragraph shows how to use dot and bracket notation to add new properties to an object. It then sets a challenge to write a dynamic sentence accessing multiple properties using dot and bracket notation, explaining the need for this.keyword to refer to the current object.

Mindmap

Keywords

💡dot notation

The dot notation is used to access properties of an object. For example, to access the lastName property of the Jonas object, we can write 'Jonas.lastName'. This is a clean and easy way to retrieve data from objects.

💡bracket notation

The bracket notation allows us to access properties dynamically by putting an expression inside the brackets. For example, 'Jonas["last" + "Name"]' will evaluate to 'Jonas.lastName'. We need this when we want to access properties based on a dynamic computation.

💡undefined

Undefined is the value we get when trying to access a non-existent property on an object. For example, Jonas does not have a 'location' property, so Jonas.location would yield undefined. We can use this to handle invalid user input.

💡expressions

Expressions produce a value, like 3+4 or a+b. We can put expressions inside brackets to dynamically access a property, like Jonas[userInput]. The expression gets evaluated first before the property lookup happens.

💡arrays

Arrays are data structures that hold ordered, list-like data. The Jonas object has a friends property which contains an array. We can get the number of elements with friends.length and access elements using index, like friends[0].

💡objects

Objects allow us to store data in a structured format with key-value pairs. For example, the Jonas object stores data like firstName, lastName, age etc. Objects help organize and access data.

💡properties

Properties are the key-value pairs in objects. For example, 'firstName' and 'Jonas' are a property in the Jonas object. The key (firstName) maps to the value (Jonas). Properties help add meaning and structure to data in objects.

💡methods

Methods are actions we can perform on objects. For example arrays have built-in methods like push and pop that add/remove elements. length and toString are some other useful methods we used.

💡operators

Operators (like +, -) allow us to perform computations and actions. The . and [] we use to access object properties are also operators with specific rules. Understanding operator precedence helps read complex code.

💡syntax

Syntax refers to the precise Structures and formats we use to write code. Getting the syntax right is key for accessing properties correctly with dot vs bracket notation, calling built-in methods properly etc.

Highlights

There are two ways to retrieve data from objects: dot notation and bracket notation.

Dot notation is more convenient when property name is known, bracket notation allows computing property name.

Bracket notation allows expressions that evaluate to property name, dot notation needs actual property name.

Undefined is returned when trying to access a non-existent property on an object.

Bracket notation evaluates the expression inside the brackets to get the property name.

Prompt function creates a popup to get user input that is returned as a string.

Conditional checks if property access returns undefined to handle invalid requests.

Dot and bracket notation can also be used to add new properties to objects.

Length property returns number of elements for arrays.

Square brackets access array elements, 0 indexes first element.

Dot and bracket notation have high operator precedence, executed left-to-right.

Dot chains apply operations left-to-right, replacing values along the way.

Member access and computed member access are formal terms for dot and bracket notation.

Computed in computed member access refers to ability to compute property names dynamically.

Rules govern everything in JavaScript, with time it will become second nature.

Transcripts

play00:01

In this video, you're gonna learn

play00:03

how to retrieve data from objects

play00:05

and also how to change data in objects,

play00:08

using both the dot and the bracket notation.

play00:13

So let's actually get the object here

play00:16

from the previous lecture,

play00:18

so that Jonas object.

play00:20

And I noticed that we actually

play00:22

didn't even take a look at it and a console,

play00:27

so let's quickly log it,

play00:29

but we're actually not gonna see a big surprise.

play00:33

So all it shows us it's really the same object

play00:37

that we just wrote in code.

play00:39

So it looks like this.

play00:41

So essentially just as it is here in the code,

play00:45

the only thing that's different is that the properties here

play00:48

are ordered alphabetically.

play00:50

And so that's what I mean when I said in the last lecture

play00:53

that the order of properties does not matter.

play00:57

And that's because we simply get the properties

play00:59

from the object using the property name itself.

play01:03

So that's actually the topic of this video.

play01:06

And so let me show you the two ways in which we do that.

play01:09

So the first way of getting a property from an object

play01:13

is by using the dot notation

play01:15

and that is very straight forward.

play01:19

So let's log that to console.

play01:22

And so let's say that we want to get the last name.

play01:25

So all we have to do is to write

play01:27

Jonas dot last name and that's it

play01:35

you see here is my last name

play01:38

coming straight from the Jonas object

play01:41

so Jonas dot last name.

play01:45

And this dot here is actually an operator

play01:48

which will go to this object

play01:50

and then retrieve to property

play01:52

with the name that we specified here.

play01:55

Now we can do the exact same thing

play01:57

using the brackets notation.

play02:00

So let me show that as well.

play02:03

So Jonas, and then we use brackets in a similar way

play02:07

in which we retrieve data from an array,

play02:10

but here we need to specify a string with the property name.

play02:14

So with the key, so let's do my name again,

play02:20

or actually a last name.

play02:21

And so these results should be exactly the same here.

play02:25

Now, the big difference between these two here

play02:28

is that in the bracket notation here,

play02:31

we can actually put any expression that we'd like,

play02:35

so we don't have to explicitly write the string here,

play02:39

but instead we can compute it from some operation

play02:42

because remember that an operation

play02:44

is basically an expression.

play02:46

So something that produces a value

play02:48

and so we can put that here, inside the brackets.

play02:52

So let's actually try that and imagine

play02:55

that we had a variable in which we store the repeating part

play02:59

in first name and last name.

play03:02

So we have name here in both

play03:05

and so let's store just that inside of a variable.

play03:09

And you will see my point here in a second

play03:12

this will all make sense, trust me.

play03:15

So name key, and then just name.

play03:19

So again, that's the repeating part

play03:21

in these two property names.

play03:25

And now we can use that variable

play03:27

to get both the first and the last name.

play03:31

So console dot log and then Jonas records.

play03:37

And so now here we can insert the expression.

play03:40

So let's create a string first

play03:43

and concatenate it with name key.

play03:48

And here we could have built this string

play03:50

using a template literal,

play03:52

but in this case it's not really necessary.

play03:56

So let's just copy this and then also last.

play04:01

So what will happen here

play04:02

is that as JavaScript sees this line of code,

play04:05

it will start by executing this plus operation

play04:09

and so it will create a string first name.

play04:12

And then it will go to the Jonas object

play04:15

and retrieve that property

play04:17

at the same for the last name

play04:19

here and the second line of code.

play04:22

And so that's how we get Jonas and Schmedtman.

play04:26

And doing something like this is more common

play04:28

than you might think

play04:30

and so that's why it's important that we understand

play04:34

that in the square brackets,

play04:35

we could put any expression here, okay.

play04:38

Now the same thing would not work

play04:41

with the dot operator or the dot notation.

play04:46

So we cannot write Jonas dot and then this here,

play04:51

for example, this would not work at all,

play04:58

so and unexpected string.

play05:01

And so that's the reason why we need

play05:03

the brackets notation and dot notation.

play05:06

We have to use the real final property name

play05:09

and not a computed property name.

play05:12

So for example this one,

play05:14

this one is a real property name

play05:15

as it appears in the object

play05:18

and so that's why it works here in this case.

play05:22

So in what situations should we use the dot notation

play05:26

and when do we have to use deep brackets notation?

play05:29

And I kind of already replied to that,

play05:32

but this is just to recap.

play05:34

So when we need to first compute the property name,

play05:37

like we did here with the first and last name,

play05:41

then of course we have to use the bracket notation

play05:45

in any other case, just use the dot notation,

play05:47

which looks a lot cleaner and it's also easier to use, okay.

play05:53

And now to make the need for the bracket notation

play05:56

even more clear, let me show you another example,

play06:00

which I think is gonna be really fun.

play06:02

So let's say that we don't know yet

play06:05

which property we want to show,

play06:07

and instead we get this information

play06:10

from some user interface

play06:13

so, for that we can use the prompt function.

play06:17

Remember, so we can use prompt

play06:20

and prompt is yet another built in function

play06:23

that is built into JavaScript

play06:25

and that we can use in any script.

play06:28

So here we can write a string

play06:30

and then this will create a popup window

play06:32

with an input field.

play06:33

Remember, so here we can write,

play06:37

what do you want to know about Jonas?

play06:45

choose between first name, last name, age, job and friends.

play06:57

And remember that this function will then return a string

play07:00

and this string, we just need to store into some variable.

play07:05

So let's call this one 'interested in', alright.

play07:12

And to start, let's just log this 'interested in' value

play07:17

to the console here,

play07:19

just till we see that it actually works.

play07:22

And so here is the popup

play07:24

and so now we just write one of these options here.

play07:29

So let's say job,

play07:32

and indeed now here we get job.

play07:34

And so what we want to do now

play07:36

is to basically display the job here in the console,

play07:39

because that's what the user chose.

play07:42

So how will we do that?

play07:44

Can we use the dot notation?

play07:47

So basically, can we do this?

play07:51

Jonas dot interested in,

play07:54

well, let's find out,

play07:56

but I hope that you can guess.

play08:00

And so we get undefined

play08:02

and this is something new

play08:03

that I didn't explain yet.

play08:05

And so let me tell you now that undefined is what we get

play08:09

when we try to access a property

play08:11

on an object that does not exist.

play08:14

So Jonas does not have a property

play08:16

called 'interested in', right?

play08:20

And so therefore the result

play08:22

of trying to access 'interested in' on Jonas is undefined.

play08:27

Now, what we need to do here

play08:29

is instead of the dot notation, use the brackets notation,

play08:33

because then we can put any expression here,

play08:36

which in this case will be interested in.

play08:39

And so JavaScript will now come here

play08:41

and replace 'interested in'

play08:43

with the actual value of the variable

play08:45

and then that's the one

play08:47

that will be looked up on the Jonas object.

play08:51

So let's try that again

play08:53

and now when I say job, we get a correct result, great.

play08:59

And I hope that you did understand

play09:02

the big difference between using the dot notation

play09:05

and the bracket notation

play09:06

and why the bracket notation gave us the correct result.

play09:11

So again, it's because basically this expression

play09:14

that we put here between these brackets will get evaluated

play09:18

then this one here is job.

play09:21

Now, of course, we can also pass in something else.

play09:24

So something that is not here, for example,

play09:27

let's try to access the location

play09:30

and then again, we get undefined.

play09:32

And once more, that is because

play09:34

there is not a property called location on the object.

play09:39

So let's actually handle that

play09:41

using some knowledge that we already have.

play09:43

So we know that undefined is a false value, right?

play09:48

And so we can use that to our advantage now

play09:50

and create some logic that we'll print a custom string

play09:54

whenever the user tries to access

play09:56

a property that doesn't exist.

play09:59

So let's do that.

play10:03

And we can say, if Jonas, brackets, 'interested in',

play10:10

then log that to the console.

play10:13

So basically this,

play10:14

and again, I'm using now the option

play10:17

and arrow down to move this line.

play10:21

And so if this value exists,

play10:23

then it is a truth value because it is not undefined

play10:27

and so any string that is not empty

play10:29

or any number that is not zero

play10:31

will then trigger this code block here to be executed.

play10:35

Now, if one of these properties was actually empty or zero,

play10:38

we would then go to the else block,

play10:41

but let's not account for that here in this case,

play10:44

let's say that doesn't happen, all right.

play10:50

So in case that the value does not exist

play10:53

So for example, with location,

play10:55

we then get undefined

play10:57

and so in this case,

play11:00

Jonas 'interested in' is undefined, which is false

play11:04

and therefore we would go to the else block.

play11:07

And so let's say something like wrong request,

play11:14

and then we can repeat this sentence here.

play11:21

So let's try that again here.

play11:24

So location, and now we get wrong request,

play11:29

but if we try something that exists,

play11:32

then we get that result, great.

play11:35

So I think this was a fun little experiment here

play11:40

and now I hope you like that.

play11:44

And now that you know

play11:45

how to retrieve elements from an object,

play11:48

let's also learn how to use both the dots

play11:51

and the brackets notation

play11:52

to add new properties to the object.

play11:56

And so that's pretty straight forward.

play12:00

So all we have to do is Jonas dot

play12:03

and now let's actually use location

play12:06

and set it equal to something.

play12:08

So to some value.

play12:11

So I'm located in Portugal

play12:13

and so that's how we use the dot notation.

play12:16

And then we can also of course use

play12:18

the brackets notation just the same.

play12:21

So let's say my Twitter handle here,

play12:26

which is @Jonasschmedtman with just one N

play12:32

and you should definitely check that out.

play12:36

And now just to see that it worked,

play12:38

let's log it to the console one final time

play12:44

and okay, it's asking us for the field name again,

play12:53

but anyway, now we have the location

play12:56

and the Twitter here as well in the object.

play12:59

And of course here

play13:00

we could put any expression just like before.

play13:04

So it works in the exact same way, alright.

play13:09

And that's how we use the brackets and the dot notation.

play13:13

Now, finally, I have one small challenge for you

play13:16

that you can just do right now

play13:18

here at the end of this video.

play13:20

And so what I want you to do is to write a sentence

play13:23

like this one, that I'm going to show you now,

play13:25

but in a dynamic way.

play13:28

So let's just write challenge here.

play13:33

And so I want you to write 'Jonas has three friends

play13:40

and his best friend is called Michael'.

play13:47

So here, we suppose that the first friend in the array,

play13:52

so in the friends array is the best friend, okay.

play13:56

And for the record, I do have more than three friends,

play14:00

but of course, here, we need to keep it all very simple.

play14:03

So again, I want you to write this sentence,

play14:06

but without hard coding, any of these values.

play14:10

So what you need to get from the object

play14:12

is this value, this value and this value.

play14:18

And this is actually a bit hard, I would say.

play14:21

And so there's absolutely no need for you to nail this,

play14:24

but you can still try it

play14:26

and maybe you can do some of the parts.

play14:29

One hint that I can give you is that you will need to use

play14:32

multiple dots to get the number of friends, okay.

play14:36

So that's totally possible.

play14:38

And so take like five minutes to try this out now.

play14:41

So pause the video and I'll see you in a second.

play14:46

All right.

play14:48

And so this is how we would do this.

play14:52

So we want to lock this to the console, of course.

play14:55

And so we're starting with the name

play14:58

and this one is probably the most straightforward one.

play15:02

So that's just, Jonas dot first name.

play15:08

So that is where the name is stored

play15:10

then has, and then the number of friends.

play15:14

So the friends are stored in Jonas dot friends.

play15:18

Now, do you remember how to get

play15:20

the number of elements in an array?

play15:23

It is by writing the array dot length, right?

play15:27

And so let's do that.

play15:30

So Jonas dot friends.

play15:35

And so this is our array, right?

play15:38

And so on this array, we can now ask for the length.

play15:43

And so basically you can see here that length

play15:46

is also a property that is available on all race.

play15:50

So it's a property that we don't define ourselves,

play15:53

but it's automatically available.

play15:55

But now that you know about the dot notation,

play15:58

you can also start to understand that the dot length

play16:01

is just a property that is available on all a race.

play16:06

And so that's what we're doing here.

play16:08

So again, we have an array which is this one,

play16:11

and on there, we can simply then request dot length.

play16:17

Okay, let's continue the sentence now.

play16:20

And his best friend is called

play16:27

and now we need to simply get

play16:29

the first element from the array.

play16:32

And so that's again, Jonas dot friends

play16:36

and now remember, we get the first element

play16:38

of an array using square brackets zero, and that's it.

play16:44

So once more, this here is simply an array

play16:47

and so we learned that to take the first element

play16:50

we just use square brackets with zero.

play16:54

So in a sense on the race,

play16:55

we also have the brackets notation.

play17:00

Okay, so that's just test this,

play17:03

and again, this annoying popup,

play17:06

but indeed Jonas has three

play17:09

and we're just missing the friends here.

play17:11

So friends and his best friend is called Michael.

play17:17

So just what I had written here as a template.

play17:21

Now, in terms of operator proceedings,

play17:24

let's quickly check out why it actually works this way.

play17:28

So with the dots here.

play17:31

Because remember, as I said,

play17:32

the dot operator here is really just that

play17:36

it's just an operator and the same goes for the brackets.

play17:40

So I will, again, Google MDN operator proceedings,

play17:45

but the link to this table

play17:46

is also on the resources pages on my website.

play17:50

And the link to that one

play17:51

is right in the first text lecture of the course.

play17:56

But anyway, here we have date dot notation,

play18:01

which here is called member access

play18:03

and then we have the brackets notation,

play18:06

which is here called computed member access.

play18:09

And so the computed,

play18:10

is because as I mentioned multiple times now,

play18:13

because we can compute the property names

play18:16

that we want to access.

play18:18

Now, what matters here is that they both have a really high

play18:21

priority and both are left to right.

play18:25

And so that's why all of this works.

play18:27

So we have two dots here

play18:30

so Jonas dot friends dot length

play18:33

and they are executed left to right.

play18:35

And therefore the first thing

play18:37

that JavaScript does is Jonas dot friends.

play18:40

And so essentially,

play18:41

this will then be replaced with the array

play18:45

and then on that array

play18:47

is where the dot length is then applied

play18:52

and the same thing here.

play18:53

So first Jonas dot friends is done.

play18:55

And then this one is also left to right

play18:58

and so from there we take element number zero, okay.

play19:04

So there's no magic involved here

play19:07

there is rules for everything and everything makes sense.

play19:11

And again, you don't need to memorize this here,

play19:13

with time all of this will become second nature to you.