You might get a question like this, an interview regarding adding elements to their right.
As you can see here, we must try to function, which gets in the way and an element and returns in
the grave with this element at the end.
And actually, it sounds really easy, but you can solve it in different ways.
This is why you must solve it correctly and at least tell interviewer the correct way.
So typically beginners will use here as a push and then we're building something like this.
So we have a function, for example, happened and we get into arguments how Ray and our new element
and what we're doing here, we're using a report and we're passing inside our L..
And after this were written in this new array, then we just right here console.log append.
And here we're checking, okay.
For example, we have our array one tool and here our third element three.
Now let's check if it's working where the light in the page and where get in one two three.
And you think that you build this function correctly?
But here's the problem.
Actually, I want to write here our right.
So let's name it, for example, numbers and we haven't saved one and two.
And now here inside depend-, I want to just pass our numbers.
But after this, I want to console.log numbers where reloading the page and we're getting one two three
one two three.
What does it mean?
We have this append function and we're written here.
They are, right?
And most people will think that when we're using this function, we're creating new array.
This is when normally they might write new numbers.
And here we're calling our append function and we think, OK, we didn't modify our old today, but
we just get new numbers because append function returns faster the rate.
But as you can see, the stories are completely equal because actually every push modifies the old array
and it doesn't create an Uri.
And this is typically a good example of bad code in the application.
Why is that?
Just imagine you have one part of your application and you have the this numbers array.
Then at some point your column this append function to get new numbers.
But then out of the blue, you modified your old numbers, which you didn't intend to do.
This is why you think that in your application, some magic can happen just because the colon of this
function modifies some data outside of this function.
This is a bad approach how we can fix it and make it better.
We should not use here as a push, put it, is not safe.
What we want to use here instead, for example, Spread Operator began right here that we want to return
in in your right and we want to spread all elements of our array that we're getting as an argument.
And if you don't know what is spread, this is a the script six operator.
These are three dots.
And what we are doing here were spreading all elements of this array inside on your right, which essentially
means that we are creating a copy of this array.
No one to add this element.
And then the following you are right.
This is why we were just right comma element.
Let's check this out now.
I am allowed in the page and here we're getting another response.
We're getting here one, two three and here one two, which actually means this single line will return
for us new array.
And the function is completely safe because we're not modifying any data outside of this function.
This is why this implementation is much better.
If you're asked even something that trivial, like pushing elements to the array also behave exactly
the same behavior with objects.
If we're passing inside our function and object and when mutated there, then when mutate an object
outside of our function and this is bad.
And you can also use spread with an object to create a new object.
In this case, we're on the safe side.
And if you didn't know this function like here is called pure, because first of all, this function
will return the same result every single time when were calling it with the same arguments.
And secondly, it doesn't modify any variables outside of our function.
This is why this is safe, pure function.