Welcome and JS - What is an Array and how to add elements to an array

Welcome and JS - What is an Array and how to add elements to an array

·

2 min read

Hi and welcome to my blog and first blog post! Here you can follow my learning journey to become a web developer. I recently got accepted to the Front-End developer program at mediainstitutet here in Sweden. So I quitted my full-time job to follow my dream.

My primary goal with this blog is for me to document my learning. But I hope some of my posts will help and inspire others also :) My first blog post will be about arrays in JavaScript. There will be future posts about HTML, CSS, react, workflow, vscode, and much more.

Happy reading!

What is an Array and how to create one?

An array is an ordered list of values and each value has an index starting from zero. An array can store numbers, strings, and booleans.

There are two ways to declare an array.

let array = [ ] // method 1

let array = new array() // method 2

I prefer the first method.

How to add elements into an array

Push

push is probably the most used array method. push is used to add new elements to the end of an array and then it returns the new length of the array.

push.gif

Unshift

unshift add one or more elements to the beginning of an array and returns the new length of the array.

unshift.gif