You can also add elements or change the elements by accessing the index value. The unshift() method adds an element at the beginning of the array. For example, let dailyActivities = Ĭonsole.log(dailyActivities) // The push() method adds an element at the end of the array. You can use the built-in method push() and unshift() to add elements to an array. Note: Array's index starts with 0, not 1. For example, const myArray = Ĭonsole.log(myArray) // "e" Array indexing in JavaScript You can access elements of an array using indices (0, 1, 2 …). You can also store arrays, functions and other objects inside an array. Here are more examples of arrays: // empty arrayĬonst stringArray = Ĭonst newData = Note: It is recommended to use array literal to create an array. In both of the above examples, we have created an array having two elements. const array2 = new Array("eat", "sleep") You can also create an array using JavaScript's new keyword. The easiest way to create an array is by using an array literal. An array is an object that can store multiple values at once.
0 Comments
Leave a Reply. |