DEV Community

Cover image for Array_01 ABC
GiandoDev
GiandoDev

Posted on

Array_01 ABC

Alt Text
As in the code above, we may create an array by writing inside the square brackets []
You can add values ( Primitives, functions and Objects ) separate by " , " .
Since the arrays are objects, they have properties and methods, so for example, you may find how many items live inside your array by the property .length
Alt Text
Sometimes you need to get a value from an array, well you may accomplish this task in two ways:
Alt Text
Array is zero index based, so this means that the first item in the array is number 0 the second is number one and so on. In the code above we see this

const peach = [fruit.length -5] // minus 5 stand for our peach
Enter fullscreen mode Exit fullscreen mode

In fact fruit.length returns 6 that correspond to the numbers of items that we have in our array; [fruit.length -5] minus five because our peach is the second element in the array so index1 (like in the code above) so to get it we need to scale from our .length five numbers.
If we want to get the last item of the array we write:

const peach = [fruits.length -1] // 6 - 1 because the last item is index5
Enter fullscreen mode Exit fullscreen mode

If you want to set an item do this:
Alt Text
Now our peach has became a carot.

Top comments (0)