Capitalize the First Letter of a String in JavaScript

Published on

NaN

String manipulation is a fundamental aspect of programming in JavaScript.

One common task is capitalizing the first letter of a string. Whether you’re formatting user input or displaying dynamic content, knowing how to manipulate strings effectively is essential.

function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); } // Example usage: const originalString = "hello world"; const capitalizedString = capitalizeFirstLetter(originalString); console.log(capitalizedString); // Output: "Hello world"