JavaScript Array || JavaScript

JavaScript

The JavaScript Array object is a global object that is used in the construction of arrays,which are high-level, list-like objects.

JavaScript array starts with index zero (0) like in Java,C++ etc.

In Java, C++ and C# programming languages we use to declare their datatypes like int , string and we can’t multi-datatype array with these datatypes. So JavaScript has this facility of multi-datatype and the need of datatypes has been remove. We just have to assign an array ([1,2,2,”hello”]) to variable.

Example #1:

<!DOCTYPE html> 
<html> 
<body> 
<p id="array"></p> 
<script> 
var array=["Java","Python","Node","React","PHP"];
document.getElementById("array")=innerHTML=array;
</script>
</body>
</html>

Output:

Java,Python,Node,React,PHP

Example #2:

<!DOCTYPE html> 
<html> 
<body> 

<script> 
var array=["Java","Python",100,"React","PHP",200];
console.log(array);
</script>
</body>
</html>

Output:

Java,Python,100,React,PHP,200

Multi-Dimensional Array:

Multi-Dimensional array has multiple arrays in an array. If an array is two dimensional it will look like [[1,2,3],[4,5,6],[7,8,9]] , the length of array is 3 and to access this array the following table will help you:

Array[row][column] 0 Column 1 Column 2 Column
Row [0][0] (1) [0][1] (2) [0][2] (3)
Row 1 [1][0] (4) [1][1] (5) [1][2] (6)
Row 2 [2][0] (7) [2][1] (8) [2][2] (9)

Syntax to access array:

array[0][0] // return 1

Example #3:

<!DOCTYPE html> 
<html> 
<body> 
<script> 
var array=[[1,2,3],[4,5,6],[7,8,9]];
console.log(array);
</script>
</body>
</html>

Output:

(3) [Array(3), Array(3), Array(3)]
0: (3) [1, 2, 3]
1: (3) [4, 5, 6]
2: (3) [7, 8, 9]
length: 3
__proto__: Array(0)

To add the elements in an array push function is used.It will push the new elements in the last of the array

Example #4:

<!DOCTYPE html> 
<html> 
<body> 
<script> 
var array=[1,2,3];
for(var i=0;i<5;i++){
array.push([1,2,34])
}
console.log(array);
</script>
</body>
</html>

Output:

Array(8)
0: 1
1: 2
2: 3
3: (3) [1, 2, 34]
4: (3) [1, 2, 34]
5: (3) [1, 2, 34]
6: (3) [1, 2, 34]
7: (3) [1, 2, 34]
length: 8



For more Examples Related CSS,html and Java Script Visit on Coding Academics

For Web,Java and Assembly language related Semester Projects please Visit on Programming Zone.  



Leave a Reply

Your email address will not be published. Required fields are marked *