DEV Community

Rajnish Katharotiya
Rajnish Katharotiya

Posted on

2 2

Convert any string to capitalize a string in javascript

Hello JS Devs, Welcome back to a new episode of series called JavaScript useful snippets. In this series, I'm sharing shortcodes and the most common useful functions of javascript. These snippets can help you to make your development more efficient and faster. stay tuned till the end to learn something new... 😊

Javascript Useful Snippets - Capitalize()

In development, it's often that we have to make sure a particular string should be capitalized only - if it's coming from the server, at that time we can use this function. Capitalize() returns a string with the first letter in capital whichever you passed through a parameter. let's look ar the syntax...

const capitalize = ([ first, ...rest ]) => first.toUpperCase() + rest.join('');

Here, this snippet will first destruct string into an array and store the first character into a variable called "first" while all rest characters will be store into the "rest" variable in the form of an array. ( PS: if you found this destruction and ...(three dots before variable) then don't worry, it's a concept of javascript only, you can learn more on detail here .. )

Next in return, this function will do two things; the first one is to convert the "first" character into uppercase ( with default method of javascript called uppercase() ), the second one is to make a string of all array (rest) records by using join. and combines both. let's see what result will look alike...

Result :

const result = ("fooBar")  // output:  FooBar

As you see, as a result, the first character of string "f" converted into "F" (capital) and the rest of the characters are the same.

Thank you for watching/reading folks, if you found this informative and wanted to make me more content like this please support me on Patreon.

Now, Guys on the next episode I'm going to share a function to get byte size of any given string. so follow/subscribe to get notification...

Subscribe on youtube https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg

Facebook: https://www.facebook.com/KatharotiyaRajnish/

Twitter: https://twitter.com/tutorial_spot

Sentry image

Common downtime causes (w/ real Node.js examples)

Bad deploys, traffic spikes, dependency fails—our guide shows how to spot downtime early with real-world Node.js examples and smarter monitoring.

Read more →

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay