- Home
- JavaScript
- JavaScript Array
Overview -JAVASCRIPT ARRAY
JavaScript array is an object that store a collection of multiple values of different types under a single name.Β
Remember in JavaScript arrays are not primitive ( means of value type of datatype like integer, float, double or characters).
JavaScript ArrayΒ has following characteristics:
- JavaScript arrays are resizable and can contain a values of different data types.
- JavaScript arrays are zero-indexed: array elements are accessed using index value and the first element of the array is at index 0,Β the second is at 1 and so on, and the last element is at the total length of arrayΒ Β minus 1.
- JavaScript arrayΒ are not associative arrays, so element can not be accessed using strings as indexes (non negative integers)
Creating An Array
Array can be created in two ways:
- Β Using an array literal
const colors= ["red", "green"];
2. Using new keyword
const colors= new Array ("blue", "orange")
const colors = ['red', 'blue', 'yellow'];
// More examples
// empty array
const myArray = [ ];
// array of numbers
const numberArray = [ 10, 20,30, 40];
// array of strings
const stringArray = [ 'Kaushal', 'Kishan', 'Megha'];
// array with mixed data types
const mixData = ['India', 'USA', 1, true];
In JavaScript arrayΒ can also store objects, functions and arrays. for example
const myArray = [
{'name': 'kaushal'},
[1, 2 ,3],
function hello() { console.log('hello')}
];
Array length property
TheΒ length property returns the number of elements in an array and can also set the length of an array.Β array.length will return total number of element in an array andΒ array.length= 5 set the length of array to 5.Β
Accessing Array Elements
We can access array elements using indices 0,1,2 …
const myArray = ['h', 'e', 'l', 'l', 'o'];
// first element
console.log(myArray[0]); // "h"
// second element
console.log(myArray[1]); // "e"
For Detail video for array & JavaScript-
Recent Post
Var keyword in C#
- May 8, 2024
- 4 min read
What is Cloud Computing ? Definition &
- April 2, 2024
- 4 min read
Devika – An Open-Source AI Software Engineer
- March 27, 2024
- 3 min read