0
# lodash.capitalize
1
2
The lodash method `_.capitalize` exported as a standalone Node.js module. This utility converts the first character of a string to uppercase and the remaining characters to lowercase, with robust type handling and edge case support.
3
4
## Package Information
5
6
- **Package Name**: lodash.capitalize
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.capitalize`
10
11
## Core Imports
12
13
```javascript
14
var capitalize = require('lodash.capitalize');
15
```
16
17
## Basic Usage
18
19
```javascript
20
var capitalize = require('lodash.capitalize');
21
22
capitalize('FRED');
23
// => 'Fred'
24
25
capitalize('hello world');
26
// => 'Hello world'
27
28
capitalize('');
29
// => ''
30
```
31
32
## Capabilities
33
34
### String Capitalization
35
36
Converts the first character of a string to upper case and the remaining characters to lower case. Handles various data types through robust type conversion, including null/undefined values, numbers, and symbols.
37
38
```javascript { .api }
39
/**
40
* Converts the first character of `string` to upper case and the remaining
41
* to lower case.
42
*
43
* @param {string} [string=''] The string to capitalize.
44
* @returns {string} Returns the capitalized string.
45
* @example
46
*
47
* capitalize('FRED');
48
* // => 'Fred'
49
*/
50
function capitalize(string);
51
```
52
53
**Usage Examples:**
54
55
```javascript
56
var capitalize = require('lodash.capitalize');
57
58
// Basic string capitalization
59
capitalize('FRED');
60
// => 'Fred'
61
62
capitalize('hello world');
63
// => 'Hello world'
64
65
capitalize('tEST');
66
// => 'Test'
67
68
// Edge cases - null and undefined
69
capitalize(null);
70
// => ''
71
72
capitalize(undefined);
73
// => ''
74
75
capitalize('');
76
// => ''
77
78
// Type conversion - numbers
79
capitalize(123);
80
// => '123'
81
82
capitalize(0);
83
// => '0'
84
85
// Special values
86
capitalize(-0);
87
// => '-0'
88
89
// Boolean conversion
90
capitalize(true);
91
// => 'True'
92
93
capitalize(false);
94
// => 'False'
95
96
// Array conversion
97
capitalize([1, 2, 3]);
98
// => '1,2,3'
99
100
capitalize(['hello', 'world']);
101
// => 'Hello,world'
102
103
// Object conversion
104
capitalize({});
105
// => '[object Object]'
106
```
107
108
### Type Conversion Behavior
109
110
The function internally converts all input values to strings before applying capitalization:
111
112
- **Strings**: Used directly
113
- **null/undefined**: Converted to empty string `''`
114
- **Numbers**: Converted to string representation (e.g., `123` → `'123'`)
115
- **Booleans**: Converted to `'true'` or `'false'`
116
- **Arrays**: Converted using default toString (e.g., `[1,2,3]` → `'1,2,3'`)
117
- **Objects**: Converted to `'[object Object]'` unless they have custom toString
118
- **Symbols**: Converted using Symbol.prototype.toString when available
119
120
### Error Handling
121
122
The function is designed to never throw errors. All input types are handled gracefully through internal type conversion, making it safe to use with any JavaScript value.