To capitalize the first letter of a string in JavaScript, you can use the charAt
and toUpperCase
methods. Here's an example:
index.tsx118 chars4 lines
In this code snippet, we first declare a variable str
and assign it the value "hello world"
. Then, we use the charAt
method to get the first character of the string (which is "h"), call the toUpperCase
method to convert it to uppercase, and concatenate it with the rest of the string using the slice
method.
The resulting string has the first letter capitalized, as desired.
gistlibby LogSnag