3.2 Array Access - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
<v ->Now that we've learned how to get from strings to arrays,</v> let's start learning about arrays themselves. We'll start by making an array of characters by splitting on the empty string, as we saw at the end of the last section. We'll just call the array "A". (computer keyboard clacking) By the way, there is a limitation with this splitting on empty string, we'll be seeing that in chapter five. Take a look at what A is, it's just what we saw at the end of the last section, like that. All right, now let's take a look at accessing individual elements. With strings, we saw that we could use the charAt method, like this, (computer keyboard clacking) so the character at zero is B, at one is A, and so on. And recall that starting at zero like this is called zero offset. It can get a little confusing, but it's very common in computer programming so it's important to get used to it. Well, arrays work the same basic way. We can look at the zeroth element, the zero index with A, and then open bracket and the number of the index. This is an extremely common notation, you'll see this across a huge variety of different programming languages. A of one, (computer keyboard clacking) A of two, and so on. And, in fact, this same notation works directly on strings. We can replace charAt with zero one. (computer keyboard clacking) Oops. (computer keyboard clacking) Two, et cetera. (computer keyboard clacking) One of the great things about JavaScript arrays is that they're really flexible. They can contain any other types, and in fact they can contain a mix of types. This is a big step up from more rigid languages like the C programming language that lies at the heart of every Unix system. C arrays can't mix types, but JavaScript is a higher level language so it can do lots of things that languages like C can't. So let's redefine A, remember using let again gives us an error. So let's just omit the let, and we can just make a literal array using these square brackets. It's a little confusing that square brackets are used both for element access and for defining literal arrays, but you'll get used to it pretty quickly. So A equals open bracket, here's a string, (computer keyboard clacking) and then we separate it with commas, As we saw before with the output of the REPL just separates the elements with commas. We can put in an integer, really just a number, as we'll see later on in chapter four JavaScript doesn't actually distinguish between the two. And we can also put in something like this, we can put in a function call, (computer keyboard clacking) soliloquy.includes("To be"). Now this might give you an error. I think it's gonna give me an error cause I've exited the Node REPL, and so soliloquy probably isn't defined. There we go, so let's define it. (computer keyboard clacking) And I'm just gonna copy it from the tutorial. All right, I just switched over to the tutorial on my system and copied it. Oops, and I copied too many double quotes. So there's soliloquy and now we can go up arrow, and this should be Badger, 42, and true, like that. As before we can access the elements using the square bracket notation. So A of two, see if you can eyeball it. That's the third element, which is the Boolean value "true".