How would I find out if a string starts with a lowercase letter by using an 'if' statement?
Asked
Active
Viewed 1.5k times
17
-
possible duplicate of [JavaScript - checking for any lowercase letters in a string](http://stackoverflow.com/questions/2830826/javascript-checking-for-any-lowercase-letters-in-a-string) – Preet Sangha Sep 28 '10 at 20:49
3 Answers
31
If you want to cover more than a-z, you can use something like:
var first = string.charAt(0);
if (first === first.toLowerCase() && first !== first.toUpperCase())
{
// first character is a lowercase letter
}
Both checks are needed because there are characters (such as numbers) which are neither uppercase or lowercase. For example:
"1" === "1".toLowerCase() //=> true
"1" === "1".toLowerCase() && "1" !== "1".toUpperCase() //=> true && false => false
"é" === "é".toLowerCase() && "é" !== "é".toUpperCase() //=> true && true => true

Turadg
- 7,471
- 2
- 48
- 49

Daniel Vandersluis
- 91,582
- 23
- 169
- 153
-
2What's the case where you need the `toLowerCase` check? E.g., where `!= toUpperCase` isn't sufficient? – T.J. Crowder Sep 28 '10 at 20:57
-
4I highly recommend that you use `===` and `!==` to avoid type coercion errors. It is a tough habit to start but I think it is very beneficial. – ChaosPandion Sep 28 '10 at 22:11
-
@Chaos that's what happens when I post right before I need to head out. Corrected! – Daniel Vandersluis Sep 28 '10 at 22:51
-
@T.J. I wasnt sure if there was a case of a character that's isn't equivalent to character.toUpperCase() so I used both conditions, but you make a good point. – Daniel Vandersluis Sep 28 '10 at 22:53
-
1In this particular example I don't see a potential *type* coercion issue, because the [`charAt`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.4), [`toLowerCase`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.16) and [`toUpperCase`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.18) methods are guaranteed to return always a string value... – Christian C. Salvadó Sep 29 '10 at 01:02
-
@ChaosPandion: `toUpperCase` and `toLowerCase` return strings, there's no opportunity for type issues. Using `===` rather than `==` is beneficial in some situations and not in others, it's not a panacea. – T.J. Crowder Sep 29 '10 at 06:48
-
@T.J. - Although technically you are right it is really all about promoting good habits. I mean you make sure there isn't any bugs in your food right? – ChaosPandion Sep 29 '10 at 16:28
-
-
@T.J., @Chaos, I would call it a "Crockfordian" *Good* Habit ;) , *understanding* how type conversion works IMHO is a lot better than just using `===` everywhere in your code... [see also](http://stackoverflow.com/questions/3735939/jslint-expected-and-instead-saw/3736117#3736117) – Christian C. Salvadó Sep 29 '10 at 18:44
-
-
@T.J., @CMS - To be honest my obsessive use of the strict equality operators came from a deep understanding of type cohesion. I haven't actually read Crockford's book. *(I have read read snippets though as it is kind of hard to avoid.)* – ChaosPandion Sep 30 '10 at 14:29
5
seems like if a character is not equal to it's upper case state it is lower case.
var first = string.charAt(0);
if(first!=first.toUpperCase()){
first character is lower case
}

kennebec
- 102,654
- 32
- 106
- 127
-1
This seems like an appropriate use of regular expressions.
var match = myString.match(/^[a-z]/);
if (match != null) {
// good match
}

lincolnk
- 11,218
- 4
- 40
- 61
-
2That's an extremely English-centric view of the problem. Is "à" not lower case? – T.J. Crowder Sep 28 '10 at 21:06
-
you're right, and I think it would work for that case. It would not be suitable for international use. I voted for Daniel's answer since it's more useful than mine. – lincolnk Sep 28 '10 at 21:18