JavaScript String Object
The JavaScript String object acts as a wrapper for primitive data type String, which is used to store text values.
The strings can be a sequence of letters, numbers, special characters, or a combination of all.
The String object contains the below properties and methods.
For more details on strings, check JavaScript Strings.
Creating String Object
A string object can be created using the new keyword as shown below.
However, it is recommended not to use the string object, as it slows down the program processing.
const a = 'hello';
const b = new String('Hello');
console.log(a); // Prints: "hello"
console.log(b); // Prints: "Hello"
console.log(typeof a); // "string"
console.log(typeof b); // "object"
String Properties
Here is the list of properties available in the String object.
Property | Description |
length | Returns the length of a string. |
prototype | Allows you to add new properties and methods to a String object. |
String Methods
Here is the list of methods available in the String object.
Method | Description |
charAt() | Returns the character at the specified index. |
charCodeAt() | Returns the Unicode of the character at the specified index. |
concat() | Joins two or more strings, and returns a new string. |
endsWith() | Checks whether a string ends with a specified substring. |
fromCharCode() | Converts Unicode values to characters. |
includes() | Checks whether a string contains the specified substring. |
indexOf() | Returns the index of the first occurrence of the specified value in a string. |
lastIndexOf() | Returns the index of the last occurrence of the specified value in a string. |
localeCompare() | Compares two strings in the current locale. |
match() | Matches a string against a regular expression, and returns an array of all matches. |
repeat() | Returns a new string that contains the specified number of copies of the original string. |
replace() | Replaces the occurrences of a string or pattern inside a string with another string, and returns a new string without modifying the original string. |
search() | Searches a string against a regular expression, and returns the index of the first match. |
slice() | Extracts a portion of a string and returns it as a new string. |
split() | Splits a string into an array of substrings. |
startsWith() | Checks whether a string begins with a specified substring. |
substr() | Extracts the part of a string between the start index and the number of characters after it. |
substring() | Extracts the part of a string between the start and end indexes. |
toLowerCase() | Converts a string to lowercase letters. |
toUpperCase() | Converts a string to uppercase letters. |
toLocaleLowerCase() | Converts a string to lowercase letters, according to the host machine's current locale. |
toLocaleUpperCase() | Converts a string to uppercase letters, according to the host machine's current locale. |
toString() | Returns a string representing the specified object. |
trim() | Removes whitespace from both ends of a string. |
valueOf() | Returns the primitive value of a String object. |
Overall
JavaScript String Object is used to create a string object that can store text values.