find whether first letter of a string is capital or not

javascript, jquery

Solution

You can use String.charCodeAt(). This function returns a a unicode value of the character. Uppercase letters have a different value than lowercase ones.

Check the docs: http://dochub.io/#javascript/string.charcodeat

function isFirstLetterCapital(string) {
  return string.charCodeAt(0) === string.toUpperCase().charCodeAt(0);
}

Edit:

charAt is actually faster - http://jsperf.com/string-operations-order. Thanks Cerbrus for pointing this out.

function isFirstLetterCapital(string) {
  return string.charAt(0) === string.charAt(0).toUpperCase();
}

Problem

Possible Duplicate: Check if first letter of word is a capital letter I want to write a function in which I pass string as argument. Then I want to find out whether the first letter of this string is capital or not. If it is capital then return true otherwise return false. How can I achieve this in javascript? Any simple demo, I will later modify according to my need.

Original source

Related problems