1
00:00:00,353 --> 00:00:03,333
Let's talk about symbols in JavaScript.

2
00:00:03,333 --> 00:00:06,060
You probably rarely use or see a symbol in

3
00:00:06,060 --> 00:00:08,353
code, but it's good to know what it is.

4
00:00:08,353 --> 00:00:11,642
Let's take a look. A symbol in JavaScript

5
00:00:11,642 --> 00:00:14,907
is a primitive data type that can be used

6
00:00:14,907 --> 00:00:17,313
as an identifier for object properties.

7
00:00:17,313 --> 00:00:19,584
Because this use case is pretty rare, I

8
00:00:19,584 --> 00:00:21,746
don't need it in the loan application, and

9
00:00:21,746 --> 00:00:24,567
so I'll explain symbols right here in

10
00:00:24,567 --> 00:00:28,046
these slides. Symbols are globally unique

11
00:00:28,046 --> 00:00:31,336
unguessable values. No symbol is exactly

12
00:00:31,336 --> 00:00:33,660
the same, and as developers, we have no

13
00:00:33,660 --> 00:00:36,576
way of knowing what the unique value of a

14
00:00:36,576 --> 00:00:39,274
symbol is. Let's explore that. This is the

15
00:00:39,274 --> 00:00:41,723
way that you can create a symbol. Notice

16
00:00:41,723 --> 00:00:44,565
that you don't use the new keyword. You

17
00:00:44,565 --> 00:00:46,333
just create one, like calling a function.

18
00:00:46,333 --> 00:00:49,009
Optionally, you can pass a string to the

19
00:00:49,009 --> 00:00:52,154
symbol when you create it. This string can

20
00:00:52,154 --> 00:00:54,739
be anything you like and is a label for

21
00:00:54,739 --> 00:00:56,254
the symbol. The string is something that

22
00:00:56,254 --> 00:00:59,135
you can see in your console to help you

23
00:00:59,135 --> 00:01:02,065
with debugging. So if you would log the id

24
00:01:02,065 --> 00:01:04,323
symbol out to the console like this, you

25
00:01:04,323 --> 00:01:06,990
would see symbol because that is the data

26
00:01:06,990 --> 00:01:10,343
type. And if you would log id.toString,

27
00:01:10,343 --> 00:01:14,083
you would see Symbol(My Id). So you'd see

28
00:01:14,083 --> 00:01:16,813
the string in the console that you passed

29
00:01:16,813 --> 00:01:19,793
as a parameter. You don't see the actual

30
00:01:19,793 --> 00:01:21,800
unique value of the symbol. That will

31
00:01:21,800 --> 00:01:24,080
always be hidden to us, and that's a good

32
00:01:24,080 --> 00:01:26,690
thing because it is unique and should stay

33
00:01:26,690 --> 00:01:29,477
that way. We can see that here, if I

34
00:01:29,477 --> 00:01:31,967
create two symbols, each with the same

35
00:01:31,967 --> 00:01:34,343
description, they would still be unique.

36
00:01:34,343 --> 00:01:36,161
If I test that by logging this in the

37
00:01:36,161 --> 00:01:38,821
console, I would get false. The string

38
00:01:38,821 --> 00:01:41,023
isn't the value of the symbol. The value

39
00:01:41,023 --> 00:01:44,052
is hidden to us. And so whenever you

40
00:01:44,052 --> 00:01:47,086
create a symbol, it will be a new, unique

41
00:01:47,086 --> 00:01:50,440
symbol. But we can reuse symbols that are

42
00:01:50,440 --> 00:01:53,079
already created. You can do that with the

43
00:01:53,079 --> 00:01:55,173
Symbol.for. In this example, I get the

44
00:01:55,173 --> 00:01:58,333
symbol with a description of My Id twice,

45
00:01:58,333 --> 00:02:01,541
and when I test to see if they are the

46
00:02:01,541 --> 00:02:03,873
same, I get true because these are the

47
00:02:03,873 --> 00:02:06,378
exact same symbols retrieved from the

48
00:02:06,378 --> 00:02:09,816
global registry. All right, so symbols are

49
00:02:09,816 --> 00:02:12,036
unique, and you can retrieve existing

50
00:02:12,036 --> 00:02:15,293
symbols. So when would you use a symbol?

51
00:02:15,293 --> 00:02:17,124
Well, you can use them to create a

52
00:02:17,124 --> 00:02:19,705
property on an object, like this. The

53
00:02:19,705 --> 00:02:22,288
object has a name property and one that

54
00:02:22,288 --> 00:02:25,065
has a unique value of a symbol as its

55
00:02:25,065 --> 00:02:28,243
name. We don't know and we shouldn't care

56
00:02:28,243 --> 00:02:31,313
about what the value is. We can access the

57
00:02:31,313 --> 00:02:33,373
property using the symbol like I do here.

58
00:02:33,373 --> 00:02:35,757
You might be asking yourself why you would

59
00:02:35,757 --> 00:02:38,811
do this. Well, here's the reason. When you

60
00:02:38,811 --> 00:02:40,681
use a symbol as a property name, it

61
00:02:40,681 --> 00:02:42,858
doesn't show up when you query the

62
00:02:42,858 --> 00:02:45,537
properties of the object, so you could use

63
00:02:45,537 --> 00:02:47,747
this to create a secret property. Using a

64
00:02:47,747 --> 00:02:49,861
symbol would be a good way to signal to

65
00:02:49,861 --> 00:02:52,846
other developers that this property is

66
00:02:52,846 --> 00:02:55,703
secret. However, it isn't completely

67
00:02:55,703 --> 00:02:58,183
hidden because you can find it when you

68
00:02:58,183 --> 00:03:00,643
call Object.getOwnPropertySymbols on the

69
00:03:00,643 --> 00:03:04,196
object. So you can use symbols to create

70
00:03:04,196 --> 00:03:06,916
kind of hidden object properties. Like I

71
00:03:06,916 --> 00:03:08,833
said, in the real world, you probably

72
00:03:08,833 --> 00:03:11,203
won't use symbols in your code, and you

73
00:03:11,203 --> 00:03:17,000
will rarely see them anywhere. In any case, you now know what they are.

