Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Make the First Character of a String Uppercase in JavaScript</title> </head> <body> <p><strong>Note:</strong> Click the "Show Output" button or refresh the page to run the test again.</p> <hr> <script> // Define function to capitalize the first letter of a string function capitalizeFirstLetter(string){ return string.charAt(0).toUpperCase() + string.slice(1); } // Calling function and display the result var str1 = 'hi there!'; str1 = capitalizeFirstLetter(str1); document.write('<p>' + str1 + '</p>'); // Print: Hi there! var str2 = 'the Gods must be crazy.'; str2 = capitalizeFirstLetter(str2); document.write('<p>' + str2 + '</p>'); // Print: The Gods must be crazy. var str3 = '/app/index.html'; str2 = capitalizeFirstLetter(str3); document.write('<p>' + str3 + '</p>'); // Print: /app/index.html </script> </body> </html>