Array
Arrays are a collection of data items stored under a single name. Array provides a mechanism for declaring and accessing several data items with only one identifier, thereby simplifying the task of data management.
We use an array when we have to deal with multiple data items. Arrays are a special type of object. The typeof operator in JavaScript returns “object” for arrays.
Declaration and initialization of Array
- Using Array Literal
Syntax: – var array_name = [ ];
Declaration and initialization of Array
- Using Array Literal
Syntax: – var array_name = [value1, value2, value_n];
Declaration and initialization of Array
- Using Array Constructor
Syntax: – var array_name = new Array( );
Declaration and initialization of Array
- Using Array Constructor
This will create an empty array with 5 lengths. So this is not a good idea to use Array
Constructor if you have only a single numeric value.
Note – By default, the array starts with index 0.
Important Points
- JavaScript arrays are zero-indexed: the first element of an array is at index 0.
- Using an invalid index number returns undefined.
- It’s possible to quote the JavaScript array indexes as well (e.g., geek[‘2’] instead of geek[2]), although it’s not necessary.
- Arrays cannot use strings as element indexes but must use integers.
- There is no associative array in JavaScript. geek[“fees”] = 200;
- No advantage to use Array Constructor so better to use Array Literal for creating Arrays in JavaScript.
Accessing Array Elements
Accessing Array Elements
Access all at once
Modifying Array Elements
Removing Array Elements
Array elements can be removed using the delete operator. This operator sets the array element it is invoked on to undefined but does not change the array‟s length.
Syantx :- delete Array_name[index];
Ex:- delete dev[0];
Length Property
The length property retrieves the index of the next available position at the end of the array. The length property is automatically updated as new elements are added to the array. For this reason, length is commonly used to iterate through all elements of an array.
- Astropilot: Elevating Pilot Training to New Heights - December 28, 2024
- Discover Ranchi with Motoshare’s Bike and Car Rental Services - December 16, 2024
- Explore Mumbai with Motoshare’s Easy Bike and Car Rentals - December 16, 2024