1 line of code: How to capitalise a string
Tags: javascript, webdev, performance, codequality
Published: 2021-12-07 16:58:44


```javascript
  const capitalise = ${str.charAt(0).toUpperCase()}${str.slice(1)}`;
```

 Returns a new capitalised string.

## Optimised version ([Benchmark](https://perf.link/#eyJpZCI6Imt1bGkyaG9zZHVjIiwidGl0bGUiOiJjYXBpdGFsaXNpbmcgYSBzdHJpbmciLCJiZWZvcmUiOiJjb25zdCBkYXRhID0gXCJMb3JlbSBpcHN1bSBkb2xvciBzaXQgYW1ldCwgY29uc2VjdGV0dXIgYWRpcGlzY2luZyBlbGl0LiBOdWxsYW0gb2RpbyB0b3J0b3IsIHRpbmNpZHVudCBhdCBlbGVpZmVuZCBzaXQgYW1ldCwgbWFsZXN1YWRhIGV0IGxpYmVyby4gQ3JhcyBzZWQgZWdlc3RhcyBuZXF1ZSwgZWdldCBzdXNjaXBpdCB1cm5hLiBEb25lYyBzYWdpdHRpcyBpbiBwdXJ1cyB2aXRhZSB2ZWhpY3VsYS4gQ3VyYWJpdHVyIGZlcm1lbnR1bSBlZ2V0IHJpc3VzIHF1aXMgbHVjdHVzLiBQZWxsZW50ZXNxdWUgZWxlaWZlbmQgbWF0dGlzIGxpZ3VsYS4gU2VkIGEgbmliaCBhY2N1bXNhbiwgbHVjdHVzIGF1Z3VlIHNpdCBhbWV0LCBmaW5pYnVzIGR1aS4gRG9uZWMgc2FnaXR0aXMsIGVyYXQgaW4gc2FnaXR0aXMgdmVuZW5hdGlzLCBuaXNsIGRpYW0gcHVsdmluYXIgbnVuYywgYXQgaWFjdWxpcyBtYXVyaXMgbGVjdHVzIGF0IG5pc2kuXCIiLCJ0ZXN0cyI6W3sibmFtZSI6IkZpbmQgaXRlbSAxMDAiLCJjb2RlIjoiY29uc3QgY2FwaXRhbGlzZSA9IChzdHIpID0%2BIGAke3N0clswXS50b1VwcGVyQ2FzZSgpfSR7c3RyLnNsaWNlKDEpfWA7XG5jYXBpdGFsaXNlKGRhdGEpIiwicnVucyI6WzE1NjMwM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DAwLDEzMzkwMDAsNTQ5MDAwLDEwMDAsMzQyMDAwLDM3NDAwMCw1NDAwMCwxMDAwLDc3MjAwMCwyMzQ1MDAwXSwib3BzIjo5ODc0OTB9XSwidXBkYXRlZCI6IjIwMjEtMTItMDhUMTU6MzU6NTYuNDYzWiJ9))
```javascript
const capitalise = str => `${str[0].toUpperCase()}${str.slice(1)}`;

```

---

## The repository & npm package

You can find the all the utility functions from this series at [github.com/martinkr/onelinecode](https://github.com/martinkr/onelinecode)
The library is also published to [npm as @onelinecode](https://www.npmjs.com/package/@onelinecode/onelinecode) for your convenience.

The code and the npm package will be updated every time I publish a new article.

---

Follow me on [Twitter: @martinkr](http://twitter.com/_martinkr) and consider to [buy me a coffee](https://www.buymeacoffee.com/martinkr)

Photo by [zoo_monkey](https://unsplash.com/@zoo_monkey) on [Unsplash](https://unsplash.com/s/photos/fuji)

---

[![Subscribe to the weekly modern frontend development newsletter](https://modernfrontend.dev/banner/banner_583-111.png)](https://modernfrontend.dev/)

---