What are Arrays and why we need them?
The arrays German Translation (Reihe) is a great tool which means row/ series, yes, you are right ,we are discussing here the netflix series of movies.
Array is ordered collection of values, values are Full Swing,Mr. Robot, My Demon, Black Mirror ,the big bang theory and evil.
Each movie is termed as element and each element has a numeric position in the array , also refered as it's index.
Right now we have one type movie and movie name is a string and string in javascript is object and as we know that everything in Javascript is refered as object. Javascript arrays are untyped and array element may contain different type of element and it can be array of objects or other arrays.
const contentCreators = [
{ name: "Bhupesh Joshi", age: 30, active: true },
{ name: "Hitesh Choudhary", age: 35, active: true },
{ name: "Piyush Garg", age: 27, active: true },
];
Array is an ordered collection of element and it is stored in contiguous memory locations and javascript arrays are dynamic,everytime you create a array of 10 elements,when we have total 10 movies in one array ,for the 11th one, we will declare the double size of array which can hold upto 20 elements.
let movies=["sanju","ms-dhoni-untold-story","apple",1920,"elway","jolly llb","shabaash mithu"]
Javascript arrays are untyped and array element can be of any type, you can store "Sanju" movie on 0th index and 1th element can be "ms-dhoni-untold-story" and 2nd will be "apple" and 3rd element will be 1920.
Each value is reknowned element and each element has a numerical postion that discern as index in JavaScript.
JavaScript arrays are untyped: an array element may be of any type, and different elements of the same array may be of different types. Array elements may even be objects or other arrays, which allows you to create complex data structures, such as arrays of objects and arrays of arrays.
JavaScript arrays are zero-based and use 32-bit indexes: the index of the first element is 0, and the highest possible index is 4294967294
(2^31 ā2), for a maximum array size of 4,294,967,295 elements.
The Length Property:
Every JavaScript array has a length property and length is count of elements in array.
for(let name=0;movies.length;name++){
console.log(movies[name]);
}
** 4 Options To create an array:**
which approach you will prefer while you create an array according to your requirement. choose wisely which fullfil your need and you always require a developer who knows about the nity gitty of javaScript.
1-Array literals
The simpliest ways to create a array which is comma seperated array literal.
let movie=[];
an movies list with zero elements and ticket_of_movies
let ticket_of_movies=[1000,500,300,600];
// array of 5 movie tickets
let miss=[100_000,true,false,"coding"];
// a miscellaneous array with the numbers ,boolean and string.
const movies = [
{ name: "Mission Mangal", earnings: 291.59, hit: true },
{ name: "Housefull 4,", earnings: 200, hit: true },
{ name: "Toilet: Ek Prem Katha", earnings: 316.97, hit: true },
];
let root = 10;
let table = [root, root+10, root+20, root+30];
Array literals can contain object literals or other array literals:
const mixedPickle = [
100,
"Mango",
{ name: "three", value: 3,language:"js" },
[4, 5, 6],
];
let emptyBetween = [1,,,,,3]; // you can insert any number of commas in javascript.
Empty elements= count the commas - 1 = 5-1=4
We will prefer and suggest use the array literal to create an array.
Little overview:
The spread operator three dots(...) can be employed to create and performs the shallow copy of array elements into new array.
let price=[10,20,20,40];
let cost=[1000,...price,500]
The Array() Constructor:
let friends = new Array();
It will create a new array with no friends and it's empty array, it shows the life is empty without friends. So,array is also empty without elements.
Call the array constructor function with single numeric value:
let bottle = new Array(10);
so my water bottle has length of 10, don't think,and have you noticed and ok take a glace at next array constructor. You will understand ,where i am trying to keep your attention.
We have created a empty bottle with length 10 and you will able to fill the 1000ml of water which ranges from 100 ml,200ml ,300ml ,400ml and ...... 1000ml till the bottle is fill.
let beerBottle=new Array(100,200,300,400,500);
You have noticed that we have added the 100 ml ,200 ml, 300 ml... 500 ml ,values of alcohol in the bottle. the ml values are elements and have you noticed the pin point ,we are not able to cater need of 100 ml drinker alone , means we can't create a single valued array to solve this problem,javascript introduce the concept of Array.of() constructor function.
Array.of() method returns the empty array or empty bottle of the array and Array.of(100) will generate the 100 ml array ,so you will get a pack of array.



Top comments (0)