In this JavaScript tutorial, we are going to learn "String in JavaScript", "JavaScript Strings Example" and Different JavaScript String Methods Examples:
What are Strings in JavaScript?
- A JavaScript string is a collection of one or more than one characters.
- A string can contain letters, numbers, and symbols.
- JavaScript strings are enclosed in single quotes (') or double quotes(").
- Strings represent textual data and are one of the primitive data types.
- Let's see JavaScript strings with an Example
JavaScript String Example:
In the given code, three variables are declared: str1, str2, and str3. Each variable holds a different string value. Here's a breakdown of each line:
1. In the first line, the variable str1 is assigned the string value 'Geeks Help'. And the string is enclosed in single quotes.
2. In the second line, the variable str2 is assigned the string value "JavaScript is amazing!". This time, the string is enclosed in double quotes.
"Finally, the console.log() function is used to print the values of each variable to the console."
Creating JavaScript String
We can create javascript strings in 2 ways:
1. By string literal
The string literal is created using single and double quotes. The syntax of creating a string using string literal is given below:
2. By string object using new keyword
The syntax of creating a string object using new keyword is given below. In this, the new keyword is used to create an instance of the string.
JavaScript String Methods with Examples
1. length
It is used to find the length of a string.
Example:
2. toLowerCase()
It is used to convert the string to lowercase.
Example:
3. toUpperCase()
It is used to convert the string to uppercase.
Example:
4. charAt()
This method is used to get the character from a given string index.
Example:
5. includes()
It is used to check whether the string includes the given character or not. If the string has given character then it will return true otherwise it will return false.
Example:
6. concat()
The javascript contact() method is used to join two strings in a single string.
Example:
7. endsWith()
It is used to check whether the string is endsWith a given character or not. If it returns with the given character then it will return true otherwise it will return false.
Example:
8. replace()
This method is used to replace the first occurrence of the string.
Example:
9. split()
This javascript string method is used to split the string into array.
Example:
10. slice()
This method is used to print the string between given indexes.
Example:
11. indexOf()
It is used to find the index of the given character in a string. And it returns the first occurance.
Example:
12. lastIndexOf()
It returns the index of the last occurrence of a substring within a string.
Example:
13. trim()
This method removes whitespace from both ends of a string and it returns a new string, without changing the original string.
Example:
14. charCodeAt()
This method provides the Unicode value of a character present at the specified index.
Example:
15. search()
It searches a specified regular expression in a given string and returns its position if a match occurs. And if not match then it returns -1.
Example:
16. match()
It searches a specified regular expression in a given string and returns that regular expression if a match occurs. And if it doesn't match then it returns null.
Example:
17. substr()
It is used to fetch the part of the given string on the basis of the specified starting position and length.
Example:
18. valueOf()
The valueOf() javascript string method provides the primitive value of the string object.
Example: