Back to Code Bytes
1 min read
capitalize()

Description

The capitalize fn takes a string and returns a new string where only the very first character is converted to uppercase. The remainder of the string is left intact.

Code Byte

export const capitalize = (str: string) => {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

Example Usage:

import { capitalize } from './capitalize.util';

// 1) Simple string
console.log(capitalize('hello world')); // "Hello world"

// 2) Formatting list items
const tags = ['javascript', 'react', 'css'];
const formattedTags = tags.map(capitalize);
console.log(formattedTags); // ["Javascript", "React", "Css"]