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


This content was proof-read by Grammarly. You can Try Grammary for FREE

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>
Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: