Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Update and Search the Watchlist

In this lesson, you kept Tomás’s lineup of films up to date as it changed at both ends, and asked it whether a film was there and where.

Key Points

  • For every array method, ask two questions: what does it do to the array, and what does the call hand back, its return value?
  • push() adds an item at the end, and unshift() adds one at the start. Both change the array and hand back its new length, not the array.
  • pop() removes the last item, and shift() removes the first. Both change the array and hand back the item they removed. After shift(), every other item moves up one index.
  • const keeps lineup on the same array, but the array itself can still change.
  • includes() hands back true or false. indexOf() hands back the index of the first match, or -1 when the item isn’t there, the same not-found answer that indexOf() gives on a string.
  • at(0) hands back the first item, and at(-1) counts from the end and hands back the last.
  • join() glues every item into one string, with whatever you pass it between them. join(", ") puts a comma and a space between the titles.
  • The methods in this lesson come in two kinds: the end methods change the array, and includes(), indexOf(), at() and join() leave it alone and only hand back an answer. Methods you’ll meet later answer the two questions differently, so keep asking them.
  • A question you’ll ask again belongs in a function whose name asks it and that returns the answer, such as isLinedUp().

Final Code

Here’s the whole session in Node’s REPL, from the first lineup to the last check:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last section, you wrote Tomás’s helpers. Now the watchlist itself starts to change, as friends add films and drop them. Tomás lines up the next few films, one per movie night.

00:13 In Node’s REPL, he stores three titles in an array named lineup, in watching order. The REPL answers undefined for the const line, because the line itself has no value to show.

00:25 Kenji wants Inception too. You already know push, which adds an item at the end. The REPL shows four. That isn’t the array. It’s the array’s new length.

00:36 A lineup changes at both ends. Films leave from the front once they’ve been watched, and plans change at the back. So for every method in this lesson, ask two questions.

00:47 What does it do to the array? And what does the call hand back, its return value?

00:54 Kenji changes his mind. pop removes the last item from the array. The call hands back the item it removed, Inception.

01:03 On Friday, the friends watch Paddington 2. shift removes the first item, and hands it back too. The other films move up. Arrival was at index one, and now it’s at index zero.

01:17 So pop and shift both change the array. Neither hands back the shortened array. Each one hands back the single item it took off. And const didn’t stop any of these changes.

01:28 It keeps lineup on the same array, but the array itself can still change.

01:35 Aisha’s birthday is next, so her pick, Spirited Away, goes to the front. unshift adds an item at the start, and the other films move back one place. Your turn: pause the video and predict what this call hands back.

01:54 It hands back three. Like push, unshift hands back the array’s new length. Type lineup on its own, and the REPL shows the array itself: Spirited Away, Arrival, then Coco.

02:10 The next methods only answer questions about the array. Is Coco lined up? includes hands back true or false. Here, it’s true, so Coco is lined up.

02:22 Tomás asks that question every week, so wrap it in a function whose name asks it. isLinedUp is an arrow that takes a title and returns what includes hands back.

02:32 Now the code reads as Tomás’s question. Arrival gives true, as it’s still in the lineup. Paddington 2 gives false, because it has left the lineup.

02:44 Where is Coco? indexOf hands back the index of the first match. Here, that’s two, so two films come before it. For Inception, which Kenji dropped, indexOf hands back -1, because it isn’t there.

03:00 That’s the same not-found answer that indexOf gives on a string. Two more methods read the array. The at method, given zero, hands back the first item, tonight’s film, Spirited Away.

03:15 Given -1, at counts from the end, so it hands back Coco. The join method glues every title into one string, with whatever you pass it between them, here a comma and a space.

03:28 That’s ready for Tomás to post in the group chat. Type lineup again. It still holds the same three films, in the same order. None of these methods changed it.

03:40 So the methods in this lesson come in two kinds. The first kind changes the array. push and unshift add at an end and hand back the new length. pop and shift remove from an end and hand back the item.

03:54 The second kind, includes, indexOf, at and join, leaves the array alone and only answers. Methods you’ll meet later answer the two questions differently, so keep asking them.

04:07 Three titles make a small, toy lineup. However, the same methods keep any list up to date. Next, in Build Records With Shorthand, you’ll write a function that builds new film records for the watchlist.

Become a Member to join the conversation.