Saturday, 1 September 2018

JavaScript: Array

Array is an ordered collection of elements.

Syntax
var arrayName = [elem1, elem2 .... elemN];
var arrayName = new Array(size)

How to access element of an array?
By using index notation, you can access element of an array. Index starts from 0.

Syntax
arrayName[index]

arr[0] : Return the first element
arr[1] : Return 2nd element
…..
…..
arr[n] : Return n-1th element

array.html

<!DOCTYPE html>

<html>

<head>
    <title>Array</title>
</head>

<body>
    <script type="text/javascript">
        var arr = [2, "Hello", 123.5, true];

        document.write("arr[0] : " + arr[0] + "<br />");
        document.write("arr[1] : " + arr[1] + "<br />");
        document.write("arr[2] : " + arr[2] + "<br />");
        document.write("arr[3] : " + arr[3] + "<br />");
        document.write("arr[4] : " + arr[4] + "<br />");
        document.write(typeof arr);
    </script>
</body>

</html>

Open above page in browser, you can able to see following data.
arr[0] : 2
arr[1] : Hello
arr[2] : 123.5
arr[3] : true
arr[4] : undefined
object

What is the maximum index number in array?
Arrays in javaScript use 32-bit indexes. So the maximum array index number is ((2^32)-2) = 4294967294 (Since arrays are zero indexed).



Previous                                                 Next                                                 Home

No comments:

Post a Comment