Created
May 2, 2024 12:12
-
-
Save MateusAlvarenga/dd3e05998ed3d3b0d2e0b85a8159b393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here's your cheatsheet rewritten in Markdown format, which is ideal for documentation or note taking. | |
## JavaScript String Case Conversion Cheatsheet | |
### 1. lower case | |
Converts all the characters in the string to lower case. | |
```javascript | |
str.toLowerCase(); | |
``` | |
### 2. UPPER CASE | |
Converts all the characters in the string to upper case. | |
```javascript | |
str.toUpperCase(); | |
``` | |
### 3. snake_case | |
Converts a string into snake case, where each word is lowercased and separated by underscores. | |
```javascript | |
str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`); | |
``` | |
### 4. kebab-case | |
Converts a string into kebab case, where each word is lowercased and separated by hyphens. | |
```javascript | |
str.toLowerCase().replace(/[A-Z]/g, ' ').split(' ').join('-'); | |
``` | |
### 5. PascalCase | |
Converts a string into Pascal case, where the first letter of each word is capitalized and words are joined without spaces. | |
```javascript | |
str.replace(/\W+(.)/g, (match, chr) => chr.toUpperCase()).replace(/^\w/, (c) => c.toUpperCase()); | |
``` | |
### 6. camelCase | |
Converts a string into camel case, similar to Pascal case but the first letter of the result is in lower case. | |
```javascript | |
str.replace(/\W+(.)/g, (match, chr) => chr.toUpperCase()).replace(/^\w/, (c) => c.toLowerCase()); | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment