check if word is capitalized js code example

Example 1: javascript check if all capital letter

function isUpper(str) {
    return !/[a-z]/.test(str) && /[A-Z]/.test(str);
}

isUpper("FOO"); //true
isUpper("bar"); //false
isUpper("123"); //false
isUpper("123a"); //false
isUpper("123A"); //true
isUpper("A123"); //true
isUpper(""); //false

Example 2: how to check if the first letter of a string is capitalized or uppercase in js

/**** Check if First Letter Is Upper Case in JavaScript***/
function startsWithCapital(word){
    return word.charAt(0) === word.charAt(0).toUpperCase()
}

console.log(startsWithCapital("Hello")) // true
console.log(startsWithCapital("hello")) // false