Top 13 JavaScript String methods every JS beginner should know!!
A Thread 👇
A quick recap of JavaScript Strings:
Strings are used for storing and manipulating text in JavaScript.
Strings can be enclosed within either single quotes, double quotes, or backticks.
Here's a simple demonstration of creating Strings.
Some of the commonly used JavaScript String methods:
string.length
charAt(index)
concat()
replace()
split()
slice(start, end)
substring(start,end)
substr(start, length)
toLowerCase()
toUpperCase()
includes()
trim()
padStart() and padEnd()
1️⃣ String Length
The length property is a read-only property that returns the length of a string.
There will be no effect if we try to change it manually.
2️⃣ charAt(index)
This method provides the char value present at the specified index.
The charAt() method takes in the index as a parameter - An integer between 0 and "str.length - 1" and returns the char value at the specified index.
3️⃣concat()
The concat() method concatenates two or more strings and returns a new string containing the combined text of the strings provided.
If the arguments are not of the type string, they are converted to string values before concatenating.
4️⃣ replace()
The JavaScript string replace() method is used to replace a part of a given string with a new substring.
Note: The replace() method does not change the string it is called on. It simply returns a new string.
5️⃣ split()
This string method splits a string into a substring array, then returns that newly created array.
It takes in 2 parameters - pattern describing where each split should occur and the separator can be a simple string or it can be a regular expression.
6️⃣ slice(start, end)
The slice() method extracts and returns a part of the string from start to (excluding) the end.
If there's no second argument specified, then splice method goes till the end of the string.
Negative values from start/end is also possible.
7️⃣ substring(start,end)
This method is almost similar to slice().
The substring() method returns the part of a string between start and end.
It takes in 2 parameters: starting index and the ending index. Negative arguments are not supported, they're r=treated as 0.
8️⃣ substr(start, length)
This method takes in two parameters: first one is the starting index and the second parameter specifies the length of the extracted part.
9️⃣ toLowerCase()
The toLowerCase() method returns the string converted to lowercase.
🔟 toUpperCase()
The toUpperCase() method returns the string converted to uppercase.
1️⃣1️⃣ includes()
The includes() method performs a search to determine whether one string may be found within another string, returning true or false as appropriate.
1️⃣2️⃣ trim()
The trim() method removes whitespace from both sides of a string.
1️⃣3️⃣ String Padding- padStart() and padEnd()
The padStart() and padEnd() methods support padding at the beginning and at the end of a string.
It takes in the two-parameter:
1- targetLength: length of the final string
2- padString: The string to pad the current string with.
All string methods return a new string. They don't modify the original string.
In other words, Strings are immutable.
That's all for this thread!
In JavaScript, strings are of great importance and are most commonly used.
A string can represent an object as well as the primitive data type.
If you learned something new from this thread make sure to retweet it. 🔥😊
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.