Capitalize the First Letter of a String in JavaScript
This challenge focuses on a fundamental string manipulation task: capitalizing the very first character of a given string. This is a common requirement in various programming scenarios, such as formatting user input, generating titles, or ensuring consistent text presentation.
Problem Description
Your task is to write a JavaScript function that takes a single string as input and returns a new string where the first letter is capitalized, and the rest of the string remains unchanged.
Key Requirements:
- The function must accept one argument: a string.
- The function must return a new string.
- If the input string is empty, the function should return an empty string.
- If the first character is not a letter (e.g., a number, symbol, or space), it should remain unchanged, and the rest of the string should also remain unchanged.
Expected Behavior:
- Given a string like "hello", the function should return "Hello".
- Given a string like "world!", the function should return "World!".
- Given a string like "123 abc", the function should return "123 abc".
- Given an empty string "", the function should return "".
Examples
Example 1:
Input: "javascript"
Output: "Javascript"
Explanation: The first letter 'j' is capitalized to 'J'.
Example 2:
Input: "a short sentence"
Output: "A short sentence"
Explanation: The first letter 'a' is capitalized to 'A'.
Example 3:
Input: ""
Output: ""
Explanation: An empty input string results in an empty output string.
Example 4:
Input: "1st place"
Output: "1st place"
Explanation: The first character '1' is not a letter, so it remains unchanged.
Constraints
- The input will always be a string.
- The input string's length can be from 0 up to a reasonable limit (e.g., 1000 characters) for typical use cases.
- The solution should be efficient and not rely on external libraries.
Notes
Consider how you will access the first character of the string and how you will convert it to uppercase. Remember that strings in JavaScript are immutable, so you'll need to construct a new string. Think about edge cases like empty strings or strings that don't start with a letter.