JavaScript: .Array()
Part of my aspiration is to document my learning journey and simplify it as much as I can and to never stop sharing with all available means.
As someone who loves learning, I recently took a step forward on my dream to become a software engineer, by learning JavaScript which is very crucial to learn for every aspiring software engineer. In the first part of my documentation, I wrote about a javascript concept called function(). In this article, I will be simplifying a JavaScript concept called array()
Introduction to Array.
Creating Array.
Accessing elements of Array.
Array Methods.
Conclusion.
INTRODUCTION TO ARRAY
Arrays are a fundamental data structure in JavaScript. They are used to store groups of data, such as numbers, strings, objects, or even other arrays. Arrays are defined by enclosing a list of values in square brackets [] and separating the values with commas (,). It should be noted that the array is zero-indexed, which means when accessing an array the first element is [0]. Below is a typical example of an array:
A typical example of an array.
CREATING ARRAY
When creating an array there are some things to know, which include: An array is defined in an enclosed square bracket and all the elements of an array are separated with a comma inside the square bracket.
ACCESSING ELEMENTS OF ARRAY
When accessing the elements of an array, it should be noted that the elements are in the index and therefore will be accessed based on each element index. Javascript array is zero index, That is the first element is [0], the second element is [1], the third element is [2]…. and so on. Below is an example
In the above example, after declaring the array, the elements of the array are the ones inside the square bracket and it can be accessed by specifying the index of the elements inside the array. In the example above the index is [1] which is the second element.
ARRAY METHODS
Javascript provides a variety of built-in methods for working with arrays. These methods allow us to perform numerous operations on arrays, such as adding or removing elements, mapping, filtering, and more. Below are some of the array methods in JavaScript.
- Length
The length property returns the length (size) of an array, That is it returns the number of elements inside the array.
2. pop()
The pop array method is used to remove the last elements of an array and returns the value of the last element. This method changes the length of the array.
Array (Pop)
3. Push()
The push() method of an array adds a specified element to the end of an array and returns the new length of the array.
4. Shift()
The Shift() method of an array removes the first element of an array and returns the new array.
5. Unshift()
The Umshift method of Array is like the opposite of the push() method, it adds the specified elements to the beginning of an array and returns the new length of the array.
- Splice()
The splice method of splice instances changes the contents of an array by removing or replacing existing elements and/or adding new elements.
Syntax:
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2)
splice(start, deleteCount, item1, item2, /* …, */ itemN)
7. Slice()
The Slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from Start to end (end is not included) where Start and End represent the index of items in that array. NOTE: The original array will not be modified.
Conclusion
Javascript Arrays are a fundamental data structure in JavaScript. They are used to store groups of data, such as numbers, strings, objects, or even other arrays. And they are zero-indexed, which means when accessing an array the first element is [0].
Browser Compatibility
You can click on the link below to check the browser compatibility of each array method.