Count the Array Elements in JavaScript

To count the array elements in JavaScript, use length.

array.length

Example 1:

Count the array elements.

<script>
    myArray = ['dog', 'cat', 'elephant'];

    console.log(myArray.length);
</script>

Output (console):

3

Example 2:

Do something if array has at least X elements.

<script>
    myArray = ['dog', 'cat', 'elephant', 'bird', 'lion', 'tiger'];

    var x = 5;

    if(myArray.length > x) { 
        console.log('many animals');
    }
</script>

Output:

many animals