1702631424

Lets learn a bit about array with javascript


Now we will have a look at what an Array is. An Array is an Object that stores values in order. In the example above we used an Object to store our car because it had specific properties that we wanted to be able to access easily via a key. If we just want to store a list of items, then there is no need to create an Object. Instead, we can use an Array. ```js const fruitBasket = ["apple", "banana", "orange"]; ``` We access values of an array via their index. Remember that arrays start at position 0. ```js const fruitBasket = ["apple", "banana", "orange"]; console.log(fruitBasket[0]); // apple console.log(fruitBasket[1]) // banana console.log(fruitBasket[2]) // orange ``` There are many methods that we can call on an Array. Lets have a look at some of the most useful. ```js const fruitBasket = ["apple", "banana", "orange"]; //get the length of the array console.log(fruitBasket.length); // 3 // add a new value at the end of the array fruitBasket.push("pear") console.log(fruitBasket); // ["apple", "banana", "orange", "pear"] // add a new value at the beginning of the array fruitBasket.unshift("melon") console.log(fruitBasket); // ["melon", "apple", "banana", "orange", "pear"] // remove a value from the end of the array fruitBasket.pop() console.log(fruitBasket); // ["melon", "apple", "banana", "orange"]; // remove a value from the beginning of the array fruitBasket.shift() console.log(fruitBasket); // ["apple", "banana", "orange"] ``` As we can see we can easily add and remove elements from the beginning or the end of an Array with these methods. if you liked this post, come and chat with me in this room [www.chat-to.dev/chat?q=javascript-room](https://www.chat-to.dev/chat?q=javascript-room) or say something cool in the comments 👇🏻👇🏻

(0) Comments