WEBVTT

1
00:00:00.160 --> 00:00:03.380
Hi and welcome to this
video course on JavaScript.

2
00:00:03.410 --> 00:00:05.220
Let's take a closer look at common

3
00:00:05.250 --> 00:00:08.010
misunderstandings about
objects and arrays.

4
00:00:08.040 --> 00:00:09.980
It's important to understand the concept

5
00:00:10.010 --> 00:00:12.900
of primitive and reference
types in JavaScript.

6
00:00:12.930 --> 00:00:16.860
As a reminder, objects
and arrays are reference types.

7
00:00:16.880 --> 00:00:20.180
This means that even if you store an array
in a constant,

8
00:00:20.210 --> 00:00:24.260
you can still edit the array without
breaking the constant restriction.

9
00:00:24.290 --> 00:00:25.660
To demonstrate this

10
00:00:25.690 --> 00:00:27.820
let's take the following example.

11
00:00:27.850 --> 00:00:33.426
Let's create an array that's a constant -hobbies

12
00:00:33.626 --> 00:00:39.700
and let's write reading
and cooking inside this array.

13
00:00:39.730 --> 00:00:42.420
Now let's say we want to push a new

14
00:00:42.450 --> 00:00:45.300
element to the existing
array using the push method.

15
00:00:45.330 --> 00:00:50.820
So let's do hobbies push programming.

16
00:00:50.850 --> 00:00:56.580
Now if we console log
hobbies, the output will be reading,

17
00:00:56.610 --> 00:01:03.300
cooking and programming and there will be
no error about editing a constant value.

18
00:01:03.330 --> 00:01:05.850
This is because reference types only store

19
00:01:05.880 --> 00:01:10.850
an address pointing to the memory
location where the array is stored.

20
00:01:10.880 --> 00:01:14.460
When we add a new element,
this address remains unchanged

21
00:01:14.490 --> 00:01:17.770
and the constant still points
to the same location in memory.

22
00:01:17.800 --> 00:01:19.540
It's important to understand this so

23
00:01:19.570 --> 00:01:22.820
that you don't get confused
when editing a constant value.

24
00:01:22.850 --> 00:01:29.210
We're not actually editing the constant
value, but the value it's pointing to.

25
00:01:29.240 --> 00:01:32.100
So same happens with the object.

26
00:01:32.130 --> 00:01:36.020
So let's create hobbies object

27
00:01:36.050 --> 00:01:44.700
and let's say that this is Nata's
hobby and type is programming.

28
00:01:44.730 --> 00:01:48.980
If we want to add to hobbies object

29
00:01:49.010 --> 00:01:55.540
a value of age for example,
and we output the hobbies object,

30
00:01:55.570 --> 00:02:02.060
we're going to see age appear here
because objects are also reference types.

31
00:02:02.090 --> 00:02:05.420
However, if you create number for example,

32
00:02:05.450 --> 00:02:08.980
and USA is twelve,
which is a primitive type,

33
00:02:09.010 --> 00:02:14.860
and now you want to reassign number
to 13 and we console log number.

34
00:02:14.890 --> 00:02:17.890
If we run this,
we're going to get an error saying

35
00:02:17.920 --> 00:02:23.890
that we're trying to assign something
else to our constant variables.

36
00:02:23.920 --> 00:02:25.820
So that's all for this video.

37
00:02:25.840 --> 00:02:27.880
Thanks for watching and see
you in the next one.

