1
00:00:00,330 --> 00:00:05,910
Already talked about hidden elements to their Ray and why push is not always the best approach.

2
00:00:06,090 --> 00:00:08,400
But you might also have a similar question.

3
00:00:08,640 --> 00:00:13,530
For example, here we have a task right to function which will concatenate two arrays.

4
00:00:16,900 --> 00:00:20,980
And actually, you might use here put and it will be an answer.

5
00:00:21,190 --> 00:00:24,850
This is where here you can actually write a function merge arrays.

6
00:00:25,210 --> 00:00:29,290
And here we get into a race, for example, array one and array two.

7
00:00:29,470 --> 00:00:32,580
And here you might write array one bush.

8
00:00:32,800 --> 00:00:34,240
And here you spread.

9
00:00:34,240 --> 00:00:35,230
Offer a tool.

10
00:00:35,560 --> 00:00:38,310
And after this, you can just return array one.

11
00:00:38,560 --> 00:00:43,870
And actually, not a lot of people will write code like this because here you are using input and then

12
00:00:43,870 --> 00:00:45,340
US president as a tool.

13
00:00:45,490 --> 00:00:50,770
But you might think about such approach now to see the problem, we must create, first of all, our

14
00:00:50,770 --> 00:00:51,370
result.

15
00:00:51,580 --> 00:00:57,430
And here, inside the result, we need to use two arrays array one, for example, it will be just one,

16
00:00:57,610 --> 00:01:03,520
and our array to here will be, for example, two and three and no one to call our merchandise.

17
00:01:03,670 --> 00:01:07,420
And Parsons said, first of all, array one and secondly, array two.

18
00:01:07,690 --> 00:01:10,090
And let's check in console.log what we're getting.

19
00:01:10,120 --> 00:01:11,440
So first of all, result.

20
00:01:11,710 --> 00:01:16,750
Then there's a one and there's a tool aimed at loading the page and we're getting one two three one

21
00:01:16,750 --> 00:01:17,890
two three two three.

22
00:01:18,250 --> 00:01:19,690
This is exactly the problem.

23
00:01:19,700 --> 00:01:23,440
Like it wasn't previous VIDEO Bush mutates array.

24
00:01:23,650 --> 00:01:28,090
This is way here where change in array one, even if we don't want to change it.

25
00:01:28,120 --> 00:01:31,180
This is why push in this case is not the best approach.

26
00:01:31,480 --> 00:01:37,420
What we can use here instead is, say, the array conquered, which is all the approach of JavaScript

27
00:01:37,600 --> 00:01:40,540
or just to spread operated just like we did previously.

28
00:01:40,750 --> 00:01:47,680
So instead of this return and the report, we can write here, first of all, already one dot com cat

29
00:01:47,830 --> 00:01:50,290
and we have concatenate function on their right.

30
00:01:50,470 --> 00:01:52,750
And here we're spreading our array to.

31
00:01:52,990 --> 00:01:55,120
Let's check the software to load in the page.

32
00:01:55,270 --> 00:01:57,130
Here is our result one, two three.

33
00:01:57,280 --> 00:02:00,520
And here are two one one and three two two three.

34
00:02:00,730 --> 00:02:05,860
And this is exactly the correct approach because concatenate does not mutate and rape.

35
00:02:06,100 --> 00:02:09,450
In this case, it will return for us in array.

36
00:02:09,610 --> 00:02:11,140
This is why everything is fine.

37
00:02:11,440 --> 00:02:13,390
If you don't want to use the ray, come cut.

38
00:02:13,570 --> 00:02:15,970
You can always just use a spread operator.

39
00:02:16,180 --> 00:02:19,090
So here we will use array one as a spread.

40
00:02:19,300 --> 00:02:24,190
And here we are spreading as a tool and actually the result will be exactly the same.

41
00:02:24,350 --> 00:02:25,810
We're getting one two three.

42
00:02:25,990 --> 00:02:31,630
And actually, I really like this approach because it is more flexible and you can merge not only arrays

43
00:02:31,810 --> 00:02:32,980
but also objects.

44
00:02:32,980 --> 00:02:39,160
You can add new elements here, and it is more modern if you, for example, have a question to concatenate

45
00:02:39,160 --> 00:02:40,510
three arrays or more.

46
00:02:40,630 --> 00:02:46,570
It is also possible, actually, even array can cat kind of work with several arrays, as you can see

47
00:02:46,570 --> 00:02:48,010
here inside documentation.

48
00:02:48,020 --> 00:02:53,890
Here we have three different arrays and you can write first array dot com cat and then you have secondary

49
00:02:53,920 --> 00:02:54,730
and third rate.

50
00:02:54,940 --> 00:02:59,750
And actually, it will work correctly, but actually you can do exactly the same with spread.

51
00:02:59,770 --> 00:03:04,450
Here you are, spreading at a one, then array two and here Array three if you need two.

52
00:03:04,600 --> 00:03:06,610
So both variants are possible.
