# Modifying an array

**URL:** <https://forum.codewithmosh.com/t/modifying-an-array/17335>\
**Category:** JavaScript\
**Created:** [December 30, 2022, 9:39am UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335 "2022-12-30T09:39:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuriAmancio](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@YuriAmancio](https://forum.codewithmosh.com/u/YuriAmancio)\
**Post date:** [December 30, 2022, 9:39am UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/1 "2022-12-30T09:39:48Z")

</div>

When I try to modify the array below and log the result, what it shows is the original array and not the string! The string will be shown only if I assign a variable to the output or console.log(numbers.join(’ , ') directly?

```auto
let numbers = [1, 2, 3];

numbers.join(',');

console.log(numbers);

```

and when I use the .sort method it worked.

```auto

const numbers = [2, 3, 1]

numbers.sort();
console.log(numbers);

```

so what is the criteria?

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [December 30, 2022, 10:38am UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/2 "2022-12-30T10:38:49Z")

</div>

The criteria is:  
You need to know how the function you are using will behave. It either returns the value or doesn’t return any value (in this case, it will perform the operation directly on the passed value (object/string/…).

.join() returns the modified data.  
So, you need to assign the returning value to some variable or you won’t be able to see the change.

.sort() doesn’t return anything. It just does it’s thing and after calling this method on any data, you can see the effect if you console log that data after the function call.

FYI: just look for those methods in google ([JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript)) and you will find a detailed description for the method.

Good luck

---

<div class="post-metadata">

**Author:** ![YuriAmancio](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@YuriAmancio](https://forum.codewithmosh.com/u/YuriAmancio)\
**Post date:** [December 30, 2022, 11:20am UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/3 "2022-12-30T11:20:35Z")

</div>

You’re saying that .join() returns the modified data meaning it will modified the array into a string, while .sort() won’t modified it?

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [December 30, 2022, 1:19pm UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/4 "2022-12-30T13:19:45Z")

</div>

> [@YuriAmancio](#):
>
> u’re saying that .join() returns the modified data meaning it will modified the array into a string, while .sort() won’t modified it?

when you use _.join()_ method, you are expected to save the returned output like:

```auto
let result = numbers.join(',');
// then probably do something with the output.
console.log(result)

```

while _.sort()_ will modify the data you are passing to the method directly without having to save it in any variable like above.  
so,

```auto
let numbers = [3, 1, 10, 4, 7];
numbers.sort(); // notice that I haven't saved the result in any variable here.
console.log(numbers);
// expected output: [1, 3, 4, 7, 10]
// but it won't sort number as above.
// this is funnier part of JavaScript. sort() will treat those as strings and then do the comparison. 

// to sort numbers:
numbers.sort( (a, b) => a-b );

```

Is that clear?

---

<div class="post-metadata">

**Author:** ![YuriAmancio](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@YuriAmancio](https://forum.codewithmosh.com/u/YuriAmancio)\
**Post date:** [December 31, 2022, 6:21pm UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/5 "2022-12-31T18:21:25Z")

</div>

> [@maverick](#):
>
> ```auto
> 
> // to sort numbers:
> 
> ```

for me both methods would modified the data, join() and sort() would both modified it somehow.  
Anyways…

```auto
let numbers = [3, 1, 10, 4, 7];
//because you add 10, it will come second in the output...very weird...and thanks for the sort numbers code.

```

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [January 1, 2023, 4:04am UTC](https://forum.codewithmosh.com/t/modifying-an-array/17335/6 "2023-01-01T04:04:23Z")

</div>

JavaScript is weird but if you use best practices and test your code, you can expect good results.

Happy to help and happy coding.
