Joe Consterdine
Joe Consterdine

Reputation: 1053

.join() works in one example but not the other

Just a quick question for the following example of reversing a string.

In the first example this works fine:

let name = ['j', 'o', 'e'].reverse().join('');
console.log(name);

But in this second example join doesn't work and name remains an array. I'm not sure why.

let name = ['j', 'o', 'e'];
name.reverse();
name.join('');
console.log(name);

Upvotes: 1

Views: 30

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386654

Array#join returns a string and you need an assignment for keeping the value.

Array#reverse woprks in situ and mutates the array.

BTW, name is a property of window and keeps the name of the window.

let value = ['j', 'o', 'e'];
value.reverse();
value = value.join('');

console.log(value);

Upvotes: 1

Related Questions