How to add an element to the end of an array in JavaScript?
Hello everyone! I just started learning JavaScript and ran into a problem. I have an array with user names:
let users = ['Anna', 'Boris', 'Victor'];
I want to add a new name 'Gleb' to the end of this array, but I don't understand how to do it correctly. I tried writing something like:
users = users + 'Gleb';
But as a result, I get a strange string "Anna,Boris,VictorGleb" instead of an array. Then I tried using square brackets:
users[users.length] = 'Gleb';
This seemed to work, but I'm not sure if this is the correct way. I also heard about some methods like push, pop, shift, unshift, but I'm confused. Please tell me how to professionally add elements to the end of an array? And is there a difference between No answers yet.
Log in to leave an answer
Blogs
Generics in Rust: Why They Are Needed and How to Use Them
IEnumerable<T> vs IQueryable<T>: what to choose in C#? Full breakdown
Concatenating a String and a Number in C++: A Complete Guide for Beginners
WebAssembly and Swift: A Powerful Tandem for Web Development