Capitalizing Strings in JavaScript

/**
 * @description Capitalizes the first letter of a given string
 * @param {string} str - The string to capitalize
 * @returns {string|boolean} - Capitalized string or false if invalid
 */
function capitalizeFirstLetter(str) {
    if (typeof str !== "string" || str.length === 0) return false;
    
    return str.charAt(0).toUpperCase() + str.slice(1);
}

console.log(capitalizeFirstLetter("hello world")); // "Hello world"
console.log(capitalizeFirstLetter("javaScript")); // "JavaScript"
console.log(capitalizeFirstLetter(123)); // false


Leave a comment

Your email address will not be published. Required fields are marked *