Arrays in JavaScript || Types of Arrays || Methods

1 minute read
0

What are Arrays ?

 In JavaScript, an array is a facts shape that stores a group of factors in a single variable. An array can shop factors of any facts kind, inclusive of numbers, strings, and items.

Arrays in JavaScript are 0-indexed, meaning that the first detail within the array is at index zero, the second element is at index 1, and so on. You can get right of entry to character elements of an array the usage of their index, like this:

var myArray = [1, 2, 3, 4, 5]; console.log(myArray[0]); // Output: 1 console.log(myArray[2]); // Output: 3

Types of arrays in JavaScript :-






Single-dimensional arrays: Single-dimensional arrays are the most common type of array in JavaScript. They are created using a comma-separated list of values enclosed in square brackets. Each value in the list is referred to as an element of the array. Here's an example of a single-dimensional array:

var myArray = [1, 2, 3, 4, 5];

Multi-dimensional arrays: Multi-dimensional arrays are arrays that contain other arrays as elements. They can be thought of as arrays within arrays. Here's an example of a two-dimensional array:-

var myArray = [[1, 2], [3, 4], [5, 6]];

Methods in Arrays:-

JavaScript arrays have many built-in methods that allow you to perform various operations on arrays. Here are some of the most commonly used array methods in JavaScript: push(): , The push() method adds one or more elements to the end of an array and returns the new length of the array.

Example 1:-

var myArray = [1, 2, 3]; myArray.push(4); console.log(myArray); // Output: [1, 2, 3, 4]

Example 2:-

var myArray = [1, 2, 3]; var lastElement = myArray.pop(); console.log(lastElement); // Output: 3 console.log(myArray); // Output: [1, 2]


Post a Comment

0Comments
Post a Comment (0)