1
00:00:00,000 --> 00:00:04,990
We are working with arrays and in JavaScript arrays are objects, and that's one

2
00:00:05,000 --> 00:00:09,990
of those words that sounds very generic, but it has a meaning in programming.

3
00:00:10,000 --> 00:00:13,990
We are going into what objects are but for now it just means they are more than

4
00:00:14,000 --> 00:00:15,990
dumb containers of data.

5
00:00:16,000 --> 00:00:18,990
Arrays are smart. They can tell us information about themselves.

6
00:00:19,000 --> 00:00:22,990
They have behavior. They can actually do things if we ask them to.

7
00:00:23,000 --> 00:00:23,990
And what do I mean by that?

8
00:00:24,000 --> 00:00:25,990
We have kind of seen some of this already.

9
00:00:26,000 --> 00:00:28,990
Let's say I create a variable that's just a string.

10
00:00:29,000 --> 00:00:34,990
But we have seen that we can use the .length property of that string variable to

11
00:00:35,000 --> 00:00:36,990
find information about the string.

12
00:00:37,000 --> 00:00:39,990
In this case, it would pop up the length of it being 24.

13
00:00:40,000 --> 00:00:41,990
I didn't have to go and count all the letters.

14
00:00:42,000 --> 00:00:46,990
The string itself actually knew what its length was, and we can use that same

15
00:00:47,000 --> 00:00:48,990
kind of format to find information about arrays.

16
00:00:49,000 --> 00:00:53,990
Let's say I use the shorthand format to create an array here with five elements

17
00:00:54,000 --> 00:00:56,990
ranging from position 0 through position 4.

18
00:00:57,000 --> 00:00:59,990
I could do something very similar that I did with the string and

19
00:01:00,000 --> 00:01:02,990
call myArray.length.

20
00:01:03,000 --> 00:01:04,990
In this case, it will pop up an alert box with the number 5.

21
00:01:05,000 --> 00:01:08,990
That's an important idea here.

22
00:01:09,000 --> 00:01:10,990
It knows its length is 5.

23
00:01:11,000 --> 00:01:14,990
That is the length of the entire array, there are five elements in it, although

24
00:01:15,000 --> 00:01:16,990
bear in mind the highest index is 4.

25
00:01:17,000 --> 00:01:22,990
0, 1, 2, 3, 4, but we are using the same .length property.

26
00:01:23,000 --> 00:01:26,990
It's smart enough to know that when we say .length on a string, it's how many

27
00:01:27,000 --> 00:01:28,990
characters are in the string.

28
00:01:29,000 --> 00:01:32,990
When we say .length on an array, it's what's the length of the array.

29
00:01:33,000 --> 00:01:36,990
Now we have already seen how we use functions in JavaScript.

30
00:01:37,000 --> 00:01:40,990
We use the name of the function with the opening and closing parentheses.

31
00:01:41,000 --> 00:01:42,990
You can create them yourself.

32
00:01:43,000 --> 00:01:45,990
We can also use the built-in functions like alert.

33
00:01:46,000 --> 00:01:50,990
In this case, I am passing in information into that function. Or I can call a

34
00:01:51,000 --> 00:01:54,990
function, passing in information that also returns a value.

35
00:01:55,000 --> 00:01:56,990
So functions are used all over the place.

36
00:01:57,000 --> 00:02:01,990
But we also have the idea of methods and they are very, very similar, but a

37
00:02:02,000 --> 00:02:05,990
method is a function that belongs to an object.

38
00:02:06,000 --> 00:02:09,990
So instead of calling it directly, we actually use the name of the object, then

39
00:02:10,000 --> 00:02:12,990
a dot, then the name of the method.

40
00:02:13,000 --> 00:02:15,990
And when I say some method here with the opening and closing parentheses, it's

41
00:02:16,000 --> 00:02:17,990
very much like calling a function.

42
00:02:18,000 --> 00:02:21,990
But we have to say what does this method apply to, what I am doing it on, and we

43
00:02:22,000 --> 00:02:23,990
have seen this before.

44
00:02:24,000 --> 00:02:28,990
If we have a string variable, we can use the .toUpperCase method to actually

45
00:02:29,000 --> 00:02:30,990
convert that to uppercase.

46
00:02:31,000 --> 00:02:34,990
And we have several very useful ones with arrays.

47
00:02:35,000 --> 00:02:39,990
So I can create multiple arrays and then call the different methods on each array.

48
00:02:40,000 --> 00:02:41,990
So let's say I use the shorthand format here.

49
00:02:42,000 --> 00:02:44,990
I am creating a new array and just putting some random stuff in it.

50
00:02:45,000 --> 00:02:47,990
I have got an array with five elements.

51
00:02:48,000 --> 00:02:51,990
What I can then do is say myArray.reverse.

52
00:02:52,000 --> 00:02:55,990
I am using the opening and closing parentheses but I don't need to pass anything in.

53
00:02:56,000 --> 00:02:59,990
That will reverse all the elements of the array in place.

54
00:03:00,000 --> 00:03:01,990
50 had been at position 4.

55
00:03:02,000 --> 00:03:04,990
It's now at position 0 and so on.

56
00:03:05,000 --> 00:03:06,990
We have other methods like .sort.

57
00:03:07,000 --> 00:03:10,990
That will sort them according to the rules of JavaScript sorting, if it's all

58
00:03:11,000 --> 00:03:14,990
alphabetical or numerical or a mixture of the two. We have .join.

59
00:03:15,000 --> 00:03:18,990
That will take all the elements of the array and combine them with commas and

60
00:03:19,000 --> 00:03:19,990
return them as a string.

61
00:03:20,000 --> 00:03:22,990
We have useful little methods like .pop.

62
00:03:23,000 --> 00:03:27,990
If I call .pop on myArray it's going to take the last element, what's currently

63
00:03:28,000 --> 00:03:32,990
in there at oosition 4, and detach it and return that as a value.

64
00:03:33,000 --> 00:03:34,990
So we can pop something off the end of the array.

65
00:03:35,000 --> 00:03:38,990
Now the flip side of that, we also have .push.

66
00:03:39,000 --> 00:03:42,990
In this case I have to push a value on to the array and it will push it to the

67
00:03:43,000 --> 00:03:44,990
last element of the array.

68
00:03:45,000 --> 00:03:48,990
So these arrays are smart. They can tell us information, they have behavior.

69
00:03:49,000 --> 00:03:52,990
We can actually get to different pieces of them. And there is more that an array can do.

70
00:03:53,000 --> 00:03:55,990
Now one of the questions you might have is when you're learning programming you

71
00:03:56,000 --> 00:03:58,990
might be thinking, "Am I supposed to remember all this?"

72
00:03:59,000 --> 00:04:00,990
Well no, at least not initially.

73
00:04:01,000 --> 00:04:04,990
You'll find that you will memorize the things that you've used but no

74
00:04:05,000 --> 00:04:10,990
programmer tries to memorize every single method on every single object. There are just too many.

75
00:04:11,000 --> 00:04:13,990
So the kind of thing that you do is you find a reference guide.

76
00:04:14,000 --> 00:04:16,990
Now with JavaScript I'm a big fan of the reference guide

77
00:04:17,000 --> 00:04:19,990
you can find at Mozilla.org.

78
00:04:20,000 --> 00:04:24,990
These are really the people that own JavaScript so it's a good example.

79
00:04:25,000 --> 00:04:28,990
We jump into that Reference guide and what I will find is it breaks down all the

80
00:04:29,000 --> 00:04:30,990
different objects that are available, like array.

81
00:04:31,000 --> 00:04:34,990
I could go in and find some information about the array.

82
00:04:35,000 --> 00:04:36,990
it would give me a lot of descriptive stuff.

83
00:04:37,000 --> 00:04:40,990
But what I will find in the Table of Contents is there will be a section that

84
00:04:41,000 --> 00:04:46,990
says Properties, things like Length or Methods, and I could jump down to Methods

85
00:04:47,000 --> 00:04:50,990
and it would actually tell me, as I start to scroll down here, I have got the pop

86
00:04:51,000 --> 00:04:53,990
method which removes the last element from an array and returns that element.

87
00:04:54,000 --> 00:04:55,990
I have got the push method.

88
00:04:56,000 --> 00:04:58,990
That will add one or more elements to the end of the array.

89
00:04:59,000 --> 00:05:02,990
I have got sort, I have got splice, and this is the way that you start

90
00:05:03,000 --> 00:05:04,990
navigating the language.

91
00:05:05,000 --> 00:05:07,990
You will find a reference guide for the language that you're working with and

92
00:05:08,000 --> 00:05:12,990
you start to explore the different pieces of the language as and when you need them.

93
00:05:13,000 --> 00:05:16,990
Don't try and memorize everything, because that's just not going to work.

94
00:05:17,000 --> 00:05:22,990
Just know where you can find the reference of the syntax. The ability to find

95
00:05:23,000 --> 00:05:26,990
what you need to in a reference guide is much more powerful than trying to

96
00:05:27,000 --> 00:05:37,000
memorize everything.