I
I
Ingerniated2018-02-19 09:02:16
JavaScript
Ingerniated, 2018-02-19 09:02:16

Why is it necessary to create a new variable every time when working with methods?

Good morning, tell me why you can not write like this?

var str = "car";
str.split().splice(1,0, "is black");

or so
var str = "car";
var res = str.split().splice(1,0, "is black");

And requires precisely so, in two lines.
var str = "car";
var res = str.split();
res.splice(1,0, "is black");

But even continuing such a chain, we cannot write
var str = "car";
var res = str.split();
res.splice(1,0, "is black");
res.join("");

And forced to create a new variable forres.join("")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-19
@Ingernirated

splice modifies an array, but returns not the modified array, but what was removed. You have 0 elements removed, so the return value is an empty array.
It's not clear why you use splice at all in this example.
You can throw everything away and write simply:
"car is black"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question