The blog describes the Array creation in JavaScript. An array is a group of element values. The JavaScript supports array creation in 3 ways
- By using Array Literals
- By creating Array Instance
- By creating Array Constructor
JavaScript Array creation using Literals
The JavaScript allows to create array using literals. The array is defined with [ ] with the list of values. The below given is the example of array using literals
let student = ["Mohit", "Rohit", "Nitin"]; alert( student[0] ); // returns Mohit alert( student[1] ); // returns Rohit alert( student[2] ); // returns Nitin
Array allow to replace the element value at nth position
student[2] = 'John';
Array allows to add new element to an existing array object
student[3] = 'Robert';
Array length can be determined
alert( student.length ); // 3
An array can support mix of values
// mix of values
let student = [ '30', { name: 'Mohit Sharma' }, true, function() { alert('stuent is enrolled for the course'); } ];
// get object at index 1
alert( student[1].name ); // Mohit Sharma
// get the function at index 3
student[3](); // prints stuent is enrolled for the courseEmpty array creation in JavaScript
JavaScript allows supports empty array as given below
let student = new Array(); // empty array using new Array() let student = []; // empty array using literals
Array creation using new keyword
The JavaScript allows to create object using new keyword
var student =new Array();
<script>
var count;
var student = new Array();
student[0] = "Mohit";
student[1] = "Rohit";
student[2] = "Vivek";
for (count=0;count<student.length;count++){
document.write(student[count] + "<br>");
}
</script> Array creation using constructor
The JavaScript allows to create object using constructor
<script>
var count;
var student = new Array("Mohit", "Rohit", "Vivek");
for (count=0;count<student.length;count++){
document.write(student[count] + "<br>");
}
</script> | Methods | Description |
|---|---|
| concat() | method allows to concatenate two or more arrays Syntax: array.concat(array1,array2,….,arrayn) Return: Combined values of all arrays |
| copywithin() | method copies the part of the array elements and returns the modified array Syntax: array.copyWithin(target, start, end) Return: the modified array elements |
| entries | method allows to iterate over each key/value pair in an array Syntax: array.entries() Return: the newly created array iterator object |
| every() | method validates if all the array elements are satisfying the provided function conditions. Syntax: array.every(callback(currentvalue,index,arr),thisArg) Return: Boolean value |
| flat() | The method concatenates all the elements of the given multidimensional array and flattens it into 1 dimensional array recursively till the specified depth. Syntax: arr.flat(<depth>); Return: new array containing all the sub-array elements of the multi dimensional array |
| flatMap() | method maps array elements via mapping function and results into a new array. Syntax: arr.flatMap(function callback(currentValue[ , index[ , array]]) Return: new array where each element is the result of the callback function |
| fill() | method fills array elements with static values Syntax: arr.fill(value[, start[, end]]) Return: the modified array |
| from() | method creates a new array with copied element of another array Syntax: Array.from(object,map_fun,thisArg); Return: return new array object |
| filter() | method returns the new array containing the elements that pass the provided function conditions. Syntax: array.filter(callback(currentvalue,index,arr),thisArg) Return: New array with filtered array elements |
| find() | method returns the value of the first array element that satisfies the specified condition |
| findIndex() | method returns the index value of the first element that satisfies the specified condition |
| forEach() | method invokes the provided function once for each element of an array |
| includes() | method validates if given array contains the specified element |
| indexOf() | method searches the specified array element returns the index of the first match |
| isArray() | method validates if the passed value is an array |
| join() | method joins the array elements as a string |
| keys() | method creates an iterator object that contains loops through the given array keys |
| lastIndexOf() | method searches the specified element in the given array and returns the index of the last match |
| map() | method calls the specified function for every array element and returns the new array |
| of() | method creates a new array from a variable number of arguments, holding any type of argument |
| pop() | method removes and returns the last array element |
| push() | method adds one or more elements to the end of an array |
| reverse() | method reverses the array elements |
| reduce(function,initial) | array executes a provided function for each value from left to right and reduces the array to a single value |
| reduceRight() | array executes a provided function for each value from right to left and reduces the array to a single value |
| some() | method validates if any array element passes the test of the implemented function |
| shift() | method removes and returns the first element of an array |
| slice() | method returns a new array containing the copy of the part of the given array |
| sort() | method returns the element of the given array in a sorted order |
| splice() | method add/remove elements to/from the given array |
| toLocaleString() | method returns a string containing all the elements of a specified array |
| toString() | method converts the elements of a specified array into string form, without affecting the original array |
| unShift() | method adds one or more elements in the beginning of an array |
| values() | method creates a new iterator object carrying values for each index in the array |
JavaScript Array flat() example
// Array flat() example
<script>
var teachers=['Angela','Cameron',['Stephnie','Brida']];
var newTeachersArr=teachers.flat(); //using array flat() method
//create a new array with one dimesnioanl array
document.write("usgae of array flat() : "+newTeachersArr);
</script>JavaScript Array entries() example
<script>
var subjects =['English','Math','Science','French'];
var itr=subjects.entries();
for(var i of itr)
{
document.write(i+"</br>");
}
</script> JavaScript Array fill() example
<script>
var subjects =['English','Math','Science','French'];
var result=subjects.fill("Computer Science");
document.writeln(arr);
</script> JavaScript Array from() example
<script>
var strText=Array.from("Converts String to Array of Alphabet"); //The string will get converted to an array.
document.write("Array contains: <br>" +strText);
</script> JavaScript Array concat(), entries() example
<script>
// Array example for concat()
var student1 = ["Mohit", "Rohit", "Vivek"];
var student2 = ["John", "Smith", "Robert"];
var totalStudents=student1.concat(student2);
document.writeln(totalStudents);
var itr=totalStudents.entries();
document.write("usgae of array entries() :"+"<br>");
for(var e of itr) //for loop using var.
{
document.write(e+"</br>");
} }
</script>