0
# lodash.upperfirst
1
2
lodash.upperfirst provides the `upperFirst` utility function that converts the first character of a string to uppercase while preserving the rest unchanged. It includes sophisticated Unicode handling with support for astral plane characters, combining marks, and zero-width joiners, making it ideal for internationalization and proper text formatting.
3
4
## Package Information
5
6
- **Package Name**: lodash.upperfirst
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.upperfirst`
10
11
## Core Imports
12
13
```javascript
14
var upperFirst = require('lodash.upperfirst');
15
```
16
17
## Basic Usage
18
19
```javascript
20
var upperFirst = require('lodash.upperfirst');
21
22
// Basic string capitalization
23
upperFirst('fred');
24
// => 'Fred'
25
26
// Already uppercase
27
upperFirst('FRED');
28
// => 'FRED'
29
30
// Empty string
31
upperFirst('');
32
// => ''
33
34
// Unicode characters
35
upperFirst('émilie');
36
// => 'Émilie'
37
38
// Works with null/undefined
39
upperFirst(null);
40
// => ''
41
42
upperFirst(undefined);
43
// => ''
44
```
45
46
## Capabilities
47
48
### String Case Conversion
49
50
Converts the first character of string to upper case with full Unicode support.
51
52
```javascript { .api }
53
/**
54
* Converts the first character of `string` to upper case.
55
*
56
* @param {string} [string=''] The string to convert.
57
* @returns {string} Returns the converted string.
58
*/
59
function upperFirst(string)
60
```
61
62
**Parameters:**
63
- `string` (string, optional): The string to convert. Defaults to empty string if not provided.
64
65
**Returns:**
66
- (string): Returns the converted string with first character in uppercase
67
68
**Key Features:**
69
- **Unicode-aware**: Properly handles complex Unicode sequences including emojis, accented characters, and combining marks
70
- **Astral plane support**: Correctly processes characters from the astral planes (U+10000 to U+10FFFF)
71
- **Zero-width joiner handling**: Preserves zero-width joiners and variation selectors in complex scripts
72
- **Null-safe**: Gracefully handles null, undefined, and non-string inputs by converting them to empty strings first
73
- **Performance optimized**: Uses efficient regex patterns and optimized string processing for both ASCII and Unicode text
74
75
**Unicode Examples:**
76
77
```javascript
78
// Emoji with skin tone modifiers - emoji stays unchanged (no uppercase form)
79
upperFirst('👨🏽💻developer');
80
// => '👨🏽💻developer' (emoji treated as single character, has no uppercase)
81
82
// Complex Unicode sequences
83
upperFirst('नमस्ते world');
84
// => 'नमस्ते world'
85
86
// Arabic with combining marks
87
upperFirst('أهلاً');
88
// => 'أهلاً'
89
90
// Accented characters
91
upperFirst('naïve');
92
// => 'Naïve'
93
```
94
95
**Type Conversion Examples:**
96
97
```javascript
98
// Numbers are converted to strings first
99
upperFirst(123);
100
// => '123'
101
102
// Booleans are converted to strings first
103
upperFirst(true);
104
// => 'True'
105
106
// Arrays are converted to strings first
107
upperFirst([1, 2, 3]);
108
// => '1,2,3'
109
```