Now, before we move on to other concepts,
I got one other concept directly related
to objects and also arrays
that you also should be aware of
since you will see it later in the course.
And that's object or array destructuring.
For this, I'll actually give you an example
in that Console in the browser again.
Here I open a blank tab again and open the Developer Tools
to use that built-in Console as a playground.
What I'm showing you here in the browser
also works like that in Node.js though.
Let's say we have a constant named input
that is actually an array with two values,
Max and Schwarzmueller, so my first and my last name.
This could be something we're getting from some form
that was submitted by the user.
Now, if we got that array,
and we know that at different positions in that array,
we got different kinds of values,
we might want to store these individual values
in separate variables or constants
with which we can work in our code.
For example, I could add a firstName constant
and simply store input
and then the first element in input in there,
so Max in this case,
and maybe create a lastName constant
and store input one in there,
so the second element in that array.
And now we would have two separate constants
that have those values that we find in the array.
And it could be more convenient to work
with these clearly named constants
than having to refer input zero or input one
all the time in our code.
But in scenarios like this
where you wanna assign different values
in an array to different constants or variables,
you can use that feature called array destructuring.
You can create multiple variables or constants in one go
by using the const or let keyword,
and then you use square brackets here
on the left side of the equal sign,
and between those square brackets,
you choose any identifiers,
any constant or variable names of your choice.
Since firstName and lastName is already taken,
I'll go for first and last here.
And then on the right side of that equal sign,
you point at the array that you want to destructure.
And destructure does not mean that the array gets destroyed
or anything like that.
No, it will continue to exist just like that,
but it means that values will be pulled out of that array
and will also be stored in these constants or variables.
And the position here matters.
The first value you define here
will get the first value in the array, so Max in this case.
The second value will get the second element in the array,
so Schwarzmueller in this case.
And you don't have to pull out all the values that exist.
You can also just grab a couple of values if you need to,
and in this case I'm getting all the values,
and now I also got first and last
as separate constants that store values of that array.
And doing it like this here simply is quicker
than assigning constants or variables individually
as I did it before.
Now, that's array destructuring.
We have pretty much the same concept for objects.
Let's say you have an object, job,
and in there you have the job title, Developer,
and you have the location which is New York.
That's the object as we created it many times before.
Now, for similar reasons as before,
we might want to get these two values
that are in that object
and maybe store them in separate variables.
Now, we got a similar syntax as before then.
Of course, we could manually do this
and create a jobTitle which holds job.title,
but that would be a bit inconvenient.
It's some extra work.
Instead, just as before,
we can create multiple consts or variables in one go
by using curly braces on the left side of the equal sign,
and now it's curly braces instead of square brackets
because we are destructuring an object now.
And that allows us to destructure the object
which, again, won't destroy it
but allows us to pull out property values out of that object
and store them in separate constants or variables.
Now, for this, unlike in array destructuring,
here, it's not the position that matters.
Instead, we have to refer
to the properties that we wanna destructure,
for example, to title and maybe also location,
though just as with arrays,
we don't have to get all the elements.
We can also just destructure one of these properties
in case we're only interested in a title.
But here the name now is important.
We have to use one of the property names
so that JavaScript knows which property we want to extract.
And with that, we now have a brand new title constant
that has the value of the title property.
In case you want to change the name,
so you wanna get the value of the title property,
but you want to have a constant of a different name,
there also is a syntax for that,
and you can destructure by adding a colon here
and then assigning your own name like jTitle for job title.
Now, we tell JavaScript that we wanna destructure,
that we wanna get the title property value, so Developer,
and that that should be stored in a new constant
that should not be named title but instead jTitle.
If I hit Enter here,
now I have that jTitle constant which holds that value.
And that is another nice JavaScript feature
which allows us to sometimes write more concise code,
and therefore it is a feature
that can make our life as a developer easier.
And maybe you remember this uuid package
which we used a little bit earlier in this course
for generating unique ids in our Node Express code.
In that earlier course section,
we installed that package and started using it.
And, actually, I did use it a little bit different
than what you see in the official documentation.
When I required it, I did not require it like this,
but instead I just required a uuid object
on which I called the v4 method in my code.
I did that because that's also fine to do.
But what you see here in the official docs
actually uses object destructuring.
We require the uuid package
which exposes an object as it seems,
and it is an object with various methods.
But since you might only need
one of these methods in your code,
you can also destructure the object, as it's suggested here,
to pull out one specific method, the v4 method,
which is the method we also use in our code ultimately,
and then you maybe even assign your own name to it,
your own alias, under which you can use that method then.
This is something you can do to write more concise code.
Of course, the approach we used also worked,
and there is no better or worse approach,
but now you also understand syntax like this,
and now we can also use syntax like this
in the rest of the course.