Last active
October 11, 2021 08:06
-
-
Save cultmind/91c824b02e63bb33ff85d887f8b590bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ARRAY : An array is a special variable, which can hold more than one value at a time. | |
Creating an Array | |
Using an array literal is the easiest way to create a JavaScript Array. | |
Syntax: | |
const array_name = [item1, item2, ...]; | |
Example | |
const bikes = ["PULSAR", "ACTIVA", "PLEASURE"]; | |
Using the JavaScript Keyword new | |
The following example also creates an Array, and assigns values to it: | |
Example | |
const bikes = new Array["PULSAR", "ACTIVA", "PLEASURE"]; | |
ARRAY METHODS | |
toString() : The JavaScript method toString() converts an array to a string of (comma separated) array values. | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.toString() | |
Result: | |
(4)Banana,Orange,Apple,Mango | |
join() : The join() method also joins all array elements into a string. | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.join(" * ") | |
Result: | |
(4)Banana * Orange * Apple * Mango | |
shift() : The shift() method removes the first array element and "shifts" all other elements to a lower index. | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.shift(); // Removes "Banana" from fruits | |
Result: | |
(3)Orange,Apple,Mango | |
unshift() : The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements . | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.unshift("Watermelon"); // Adds "Watermelon" to fruits | |
Result: | |
(5)['Watermelon','Banana','Orange','Apple','Mango'] | |
push() : The push() method adds a new element to an array (at the end). | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.push("Kiwi"); // Adds "Kiwi" to fruits | |
Result: | |
(5)['Banana','Orange','Apple','Mango','Kiwi'] | |
pop() : The pop() method removes the last element from an array. | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.pop(); // Removes "Mango" from fruits | |
Result: | |
(3)['Banana','Orange','Apple'] | |
splice() : The splice() method can be used to add new items to an array. | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.splice(0, 0, "Lemon", "Kiwi"); | |
Result: | |
(6)['Lemon','Kiwi','Banana','Orange','Apple','Mango'] | |
slice() : The slice() method slices out a piece of an array into a new array. | |
This example slices out a part of an array starting from array element 1 ("Orange"): | |
Example 1 : | |
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; | |
const citrus = fruits.slice(1); | |
Result: | |
(4)'[Orange','Lemon','Apple','Mango'] | |
Example 2 : | |
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; | |
const citrus = fruits.slice(1, 3); | |
Result: | |
(2)['Orange','Lemon'] | |
concat() : The concat() method creates a new array by merging (concatenating) existing arrays. | |
Example | |
const myGirls = ["Cecilie", "Lone"]; | |
const myBoys = ["Emil", "Tobias", "Linus"]; | |
// Concatenate (join) myGirls and myBoys | |
const myChildren = myGirls.concat(myBoys); | |
Result: | |
(5)['Cecilie','Lone','Emil','Tobias','Linus'] | |
ARRAY SORTING | |
sort() : The sort() method sorts an array alphabetically. | |
Example 1 : | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.sort(); // Sorts the elements of fruits; | |
Result: | |
(4)['Apple','Banana','Mango','Orange'] | |
Example 2 : | |
const names = ["Superstar","Vijay","Akhil","Mahesh","Ganesh"]; | |
names.sort(); | |
Result: | |
(5)['Akhil','Ganesh','Mahesh','Superstar','Vijay'] | |
reverse() : The reverse() method reverses the elements in an array. | |
You can use it to sort an array in descending order: | |
Example | |
const fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.sort(); // First sort the elements of fruits | |
fruits.reverse(); // Then reverse the order of the elements | |
Result: | |
(4)['Orange','Mango','Banana','Apple'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment