Avatar ·

How to add an element to an array in JavaScript? (append)

Hello everyone! I just started learning JavaScript and ran into a problem. I have an array of numbers, for example: `let numbers = [1, 2, 3];`. I need to add a new number, say 4, to the end of this array. I tried writing `numbers = numbers + 4`, but the result is a string "1,2,34", not an array. Then I tried `numbers.push(4)` — it worked, but I don't fully understand why. I also heard about the `concat` method, but it seems to return a new array rather than modifying the old one. Please tell me, which method is correct for adding an element to an array? And what should I do if I need to add not just one element, but several at once? Thanks in advance for your help! <<>>

Main ways to add elements to an array

In JavaScript, there are several ways to add elements to an array.