Check if a char is alphanumeric in JavaScript

To check if a character is alphanumeric, you can use RegEx:

javascript
let isAlphanumeric;

isAlphanumeric = /[a-zA-Z0-9]/.test('.'); // false
isAlphanumeric = /[a-zA-Z0-9]/.test('n'); // true
isAlphanumeric = /[a-zA-Z0-9]/.test('R'); // true

Another alternative without RegEx:

javascript
let isAlphabetic   = char => char.toUpperCase () !== char.toLowerCase();
let isNumeric      = char => char >= 0 && char < 9;
let isAlphanumeric = char => isAlphabetic(char) || isNumeric(char);

isAlphanumeric('.'); // false
isAlphanumeric('n'); // true
isAlphanumeric('R'); // true

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.