DEV Community

Sowntharya
Sowntharya

Posted on Edited on

Array in JavaScript

Array is a group or collection of different data types stored in single variable/continuous memory.
used to organize same type of data.
Array is index based, index starts at 0.
Array is fixed in size.

1.Create a array

let fruits = ["apple", "mango", "orange"];
console.log(fruits);   

output: ["apple", "mango", "orange"]
Enter fullscreen mode Exit fullscreen mode

Array Methods ( accessing array elements )

 const fruits = ["banana" , "orange" , "apple" , "Mango" , "kiwi" ];
 let size = fruits.length;
 console.log("length of array : " + size);

 // indexOf() - Find the index
 console.log(fruits.indexOf("apple"));  // apple is index 2

 // includes() - Check if an element exists
console.log(fruits.includes("orange"));  //true
console.log(fruits.includes("Grapes"));   //false

 // add new element
 fruits[fruits.length] = "mango";
 console.log(fruits);

 //change a array element by reffering index
 fruits[0] = "lemon"; 
 console.log(fruits);

 let list = fruits.toString();
 console.log("array to string : " + list);


 let fruit = fruits.at(-1);  // reverse/ based on index / starts from 0
 console.log("third element of fruits : " +  fruit);

  //use separator
 let concat = fruits.join(" + * , "); 
 console.log("join all array element into string  : "+ concat );

 // return last value
 let rmv = fruits.pop();
 console.log("removes last elements : " + rmv); 

 //return array length
 let add =  fruits.push("starfruit");
 console.log("add new element to array at end : " + add); 

 let remove = fruits.shift();
 console.log("remove 1st array and shift all element to lower index : " +    remove);
 console.log(fruits);

 // return added index
 let unshft = fruits.unshift("strawberry"); 
 console.log("add new element and unshift older element : " + unshft);
 console.log(fruits);

 // add new method to js
 let arr = Array.isArray(fruits); 
 console.log(arr);
 console.log(fruits);

 let dlt = delete fruits[7]; // 0 index value become empty
 console.log(dlt);
 console.log(fruits);

 // merge array
 const fname = ["Sowntharya" , "sam"];
 const lname = ["Rajendran" , "sung"];
 const name = fname.concat(lname); 
 console.log(name); 

 // create new array with sub array
 const flt = [[1,2], [3,4], [4,5]];
 let arry = flt.flat(); 
 console.log(arry); 

 // 1st create new array  then create flattening array
 const map = [1,2,3,4,5,6];
 let fmap = map.flatMap(a => [a, a * 10]); 
 console.log(fmap);

 // slice() -return a part of an array without changing the original array
 let splce = fruits.splice(2,0, "guava", "pineapple");
 console.log(splce); 
 let numbers = [10, 20, 30, 40, 50];
 console.log(numbers.slice(1,4));  //[20, 30, 40]
 console.log(numbers);  //[10, 20, 30, 40, 50]


 // splice() - Add, remove or replace element 
 //remove elements: starts at index 1 and removes 1 element.
 let fruits = ["apple", "mango", "orange"];
 fruits.splice(1,1);
 console.log(fruits);  ["apple", "orange"]


Enter fullscreen mode Exit fullscreen mode

Top comments (0)