How to check if character is a letter in Javascript? How to check if character is a letter in Javascript? javascript javascript

How to check if character is a letter in Javascript?


I don't believe there is a built-in function for that. But it's easy enough to write with a regex

function isLetter(str) {  return str.length === 1 && str.match(/[a-z]/i);}


With respect to those special characters not being taken into account by simpler checks such as /[a-zA-Z]/.test(c), it can be beneficial to leverage ECMAScript case transformation (toUpperCase). It will take into account non-ASCII Unicode character classes of some foreign alphabets.

function isLetter(c) {  return c.toLowerCase() != c.toUpperCase();}

NOTE: this solution will work only for most Latin, Greek, Armenian and Cyrillic scripts. It will NOT work for Chinese, Japanese, Arabic, Hebrew and most other scripts.


if( char.toUpperCase() != char.toLowerCase() ) 

Will return true only in case of letter

As point out in below comment, if your character is non English, High Ascii or double byte range then you need to add check for code point.

if( char.toUpperCase() != char.toLowerCase() || char.codePointAt(0) > 127 )