How to use isNaN() function In Javascript
In this Approach we iterates through each character of the string, checking if it’s not a number using isNaN(). It returns true if all characters are non-numeric, indicating the string contains only alphabetic characters.
Example : In this example we defines a function isAlphabetic to check if a string consists only of alphabetic characters using the isNaN function. It tests the function with two strings and prints the results.
let str1 = "w3wiki";
let str2 = "Geeks123";
function isAlphabetic(str) {
return [...str].every(char => isNaN(char));
}
console.log(isAlphabetic(str1));
console.log(isAlphabetic(str2));
Output
true false
JavaScript Program to Check if a String Contains only Alphabetic Characters
In this article, we are going to learn about checking if a string contains only alphabetic characters in JavaScript.Checking if a string contains only alphabetic characters means verifying that all characters within the string are letters of the alphabet (A-Z or a-z) and do not include any digits, symbols, or other non-alphabetic characters.
Some common methods can be used to check if a string contains only alphabetic characters in JavaScript, which are listed below:
Table of Content
- Using a Regular Expression
- Using for-of loop
- Using every() method and charCodeAt() method
- Using isNaN() function
- Using Array.prototype.filter and String.prototype.match
- Using Array.prototype.reduce
We will explore all the above methods along with their basic implementation with the help of examples.