Back to Functions

upperFirst

string

Converts the first character of string to upper case.

Installation

Import
import { upperFirst } from '@tulx/utils';

Source Code

Implementation
/**
 * Converts the first character of string to upper case.
 *
 * @param string - The string to convert.
 * @returns Returns the converted string.
 *
 * @example
 * ```ts
 * upperFirst('fred'); // 'Fred'
 * upperFirst('FRED'); // 'FRED'
 * ```
 */
export function upperFirst(string: string): string {
  if (string.length === 0) {
    return string;
  }
  return string.charAt(0).toUpperCase() + string.slice(1);
}

Example

import { upperFirst } from '@tulx/utils';

upperFirst('fred'); // 'Fred'
upperFirst('FRED'); // 'FRED'

Related Functions