What does the following JavaScript code print (console output, as in Node.js)?
console.log([1].concat(2, [3, [4]]));
concat spreads array arguments one level deep; the inner [4] stays an array.
Arrays · Exercise 2 of 4
Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.
What does the following JavaScript code print (console output, as in Node.js)?
console.log([1].concat(2, [3, [4]]));
concat spreads array arguments one level deep; the inner [4] stays an array.
What does the following JavaScript code print (console output, as in Node.js)?
console.log([1, null, undefined, 2].join("-"));
console.log(String([[1, 2], [3]]));
join turns null and undefined into empty strings. Nested arrays are converted with their own join(","), so the nesting disappears.
What does the following JavaScript code print (console output, as in Node.js)?
const a = [1, 2, 3];
const r = a.reverse();
r.push(0);
console.log(a);
reverse changes a in place and returns a itself, so pushing to r pushes to a. Use toReversed() for a copy.
What does the following JavaScript code print (console output, as in Node.js)?
const grid = new Array(2).fill([]);
grid[0].push(1);
console.log(grid);
Both slots point to the same inner array, so the push shows up in both rows.
What does the following JavaScript code print (console output, as in Node.js)?
console.log(Array.from("abc"), Array.from(new Set([1, 1, 2])));
Strings iterate by character; a Set has already removed the duplicate 1.
What does the following JavaScript code print (console output, as in Node.js)?
console.log(Array(3).length, Array.of(3).length, Array(3, 4));
Only a single numeric argument means "length". With two arguments the constructor creates those elements; Array.of always does.
What does the following JavaScript code print (console output, as in Node.js)?
const a = [[1], [2]];
const b = [...a];
b[0].push(9);
b[1] = [0];
console.log(a);
The spread copy is a new outer array holding the same inner arrays. Pushing into b[0] changes the shared inner array; replacing b[1] changes only b.
What does the following JavaScript code print (console output, as in Node.js)?
console.log([1, 2] == "1,2", [1, 2] === [1, 2], [] + 1, [5] * 2);
Arrays convert to strings when a primitive is involved: [1, 2] → "1,2", [] → "" ("" + 1 = "1"), [5] → "5" → 5 for *. Two arrays are compared by reference.
What does the following JavaScript code print (console output, as in Node.js)?
const [first, , third = "def", ...rest] = [1, 2, undefined, 4, 5];
console.log(first, third, rest);
The empty comma skips 2. The third element is undefined, so the default "def" applies. rest gets everything after it.
What does the following JavaScript code print (console output, as in Node.js)?
const a = [1, 2, 1, 2];
console.log(a.indexOf(1, 1), a.lastIndexOf(2, 2), a.includes(2, 4));
indexOf(1, 1) starts at index 1 and finds the 1 at index 2. lastIndexOf(2, 2) searches backwards from index 2 and finds index 1. includes(2, 4) starts past the end.
Timed online tests with instant scores and explanations.