JavaScript provides the every() method defined in the Array object which lets the developers to check and validate the contents of the array in JavaScript.

How to Check if all elements in array are alphabets in JavaScript ?

Below is a sample code snippet demonstrating the usage of the every() method to find out if all the elements in the array are alphabets.

Note that every() method takes the function as parameter which validates for the character using regular expression.

<script type="text/javascript">       
    var players = new Array('Sachin', 'Virat', 'abundant');
    var retvalue = players.every(function(element, index, array) {
        var textExp = /^[a-zA-Z]+$/;
        return textExp.test(element);
    });
    console.log(retvalue);
</script>

Leave a Reply

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

Liked this content?. Please spread the word :)

  • RSS
  • Twitter
    Visit Us
    Follow Me