Joining Arrays With concat()

The Monday script can now name the people on call for the next three weeks because rota.slice(0, 3) gives Dev, Sam, and Ines. The pager has a second list, though. When the person on call can’t fix a problem alone, they escalate, and the escalation list is those same three people followed by the two managers, Priya and Tom. The script prints that list and how many people are on it, so whoever is holding the pager at 3 a.m. knows who to wake.

In Python, joining two lists takes one +. By the end of this lesson, the JavaScript version will put the right five people on the escalation list. Start with the line you’d port first.

Port the Escalation List

The Python script builds the escalation list from a slice of the rota and the managers’ list, then reports it. Here’s that step in the Python REPL:

Language: Python
>>> rota = ["Dev", "Sam", "Ines", "Kofi", "Bea", "Marta"]
>>> managers = ["Priya", "Tom"]
>>> escalation = rota[:3] + managers
>>> print(escalation)
['Dev', 'Sam', 'Ines', 'Priya', 'Tom']
>>> print(f"Escalation list: {len(escalation)} people")
Escalation list: 5 people

Python’s + on two lists builds a new list with five names, and the count agrees.

The port is a line-by-line carry-over. The slice becomes .slice(0, 3), len() becomes .length, and the + stays where it was:

Language: JavaScript
const rota = ["Dev", "Sam", "Ines", "Kofi", "Bea", "Marta"];
const managers = ["Priya", "Tom"];

const escalation = rota.slice(0, 3) + managers;
console.log(escalation);
console.log(`Escalation list: ${escalation.length} people`);

Before you run it, make a prediction. Will the first line show five names in brackets, and will the count say five?

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

Become a Member to join the conversation.