Photo by Sergey Pesterev on Unsplash
In programming, there are several practices of writing compound words or phrases. These practices are used as conventions in almost every programming language and not fix from one to another. The choice of naming conventions always be a controversial issue, with some are holding theirs to be the best and others to be inferior.
Moving forward, before we can do some string conversion, we need to recognize the case styles first. We will learn 4 different case styles which are:
1. Snake case
Photo by David Clode on Unsplash
Snake case is the practice of writing compound words in which the words are separated by one underscore character and no spaces. The first letter is either upper- or lowercase. It is commonly used in declaring variable names, function names, and sometimes computer's filenames.
2. Kebab case
In Kebab case, all letters are written in lower case and the words are separated by a hyphen or minus sign. The kebab notation is often used as a convention for naming filenames.
3. Camel case
Camel case describes the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, without intervening spaces or punctuation.
4. Pascal case
Pascal case is similar to camel case but the first letter is in uppercase. There is no space and hyphen to separate the words. The pascal case is often used as a convention in creating class in many programming language.
Let's start with the string conversion as we already recognized every case styles above.
You can start by creating a new js file called
main.js and run it in your terminal/console window by using the following command:
1. String to Snake Case
Convert string to snake case
Result of conversion to snake case
2. String to Kebab Case
Convert string to kebab case
I use the same RegEx as in conversion to snake case above.
Result of conversion to kebab case
3. String to Camel Case
Convert string to camel case
Result of conversion to camel case
4. String to Pascal Case
Convert string to pascal case
Result of conversion to pascal case
Throughout this article, we have learned on how to distinguish 4 case styles which are often seen in any programming language as a convention.
We also got the idea on how to convert a string to these 4 case styles. If you think this article was helpful, don't forget to share it with your friends.