JavaScript string is an object which represents the sequence of characters. Generally, strings are used to hold text-based values such as a person name or a product description.
In JavaScript, any text within the single or double quotes is considered as a string. There are two ways for creating a string in JavaScript:
Let us elaborate both ways of creating a string in JavaScript.
The string literals can be created by using either double quotes or by using single quotes. The syntax for creating string literal is given below:
var stringname = "string value";
Here, we will use a new keyword for creating the string object. The syntax for creating the string object is given below:
var stringname = new String ("string literal");
There are some properties of the string that are tabulated as follows:
S.no. | Property | Description |
---|---|---|
1. | constructor | It returns the constructor function for an object. |
2. | length | It returns the length of the string. |
3. | prototype | It allows us to add the methods and properties to an existing object. |
Let us discuss the above string properties in detail.
The constructor property returns the constructor function for an object. Instead of the name of the function, it returns the reference of the function.
string.constructor
var str = new String("Hello World"); console.log("Value of str.constructor is: "+str.constructor);
As its name implies, this property returns the number of characters or the length of the string.
string.length
var str = new String("Hello World"); console.log("The number of characters in the string str is: "+str.length);
It allows us to add new methods and properties in an existing object type. It is a global property which is available with almost all the objects of JavaScript.
object.prototype.name = value;
function student(name, qualification){ this.name = name; this.qualification = qualification; } student.prototype.age = 20; var stu = new student('Daniel Grint' , 'BCA'); console.log(stu.name); console.log(stu.qualification); console.log(stu.age);
There are four string functions available in ES6, which are tabulated as follows:
S.no. | Methods | Description | JavaScript Version |
---|---|---|---|
1. | startsWith | It determines whether a string begins with the characters of a specified string. | ECMAScript 6 |
2. | endsWith | It determines whether a string ends with the characters of a specified string. | ECMAScript 6 |
3. | includes | It returns true if the specified argument is in the string. | ECMAScript 6 |
4. | repeat | It returns a new string repeated based on specified count arguments. | ECMAScript 6 |
Let us discuss the above string methods in detail.
It is a case-sensitive method, which determines whether the string begins with the specified string characters or not. It returns true if the string begins with the characters and returns false if not.
string.startsWith(searchValue, startPosition)
This method includes two parameters that are as follows:
var str = 'Welcome to rookienerd :)'; console.log(str.startsWith('Wel',0)); console.log(str.startsWith('wel',0));
It is also a case-sensitive method that determines whether a string ends with the characters of the specified string or not.
string.endsWith(searchvalue, length)
The parameters of this method are defined as follows:
var str = "Welcome to rookienerd."; console.log(str.endsWith("to", 10)) console.log(str.endsWith("To", 10))
It is a case-sensitive method that determines whether the string contains the characters of the specified string or not. It returns true if the string contains characters and returns false if not.
string.includes(searchValue, start)
Let's understand the parameters of this method.
let str = "hello world" console.log(str.includes('world',5)); console.log(str.includes('World', 11))
It is used to build a new string that contains a specified number of copies of the string on which this method has been called.
string.repeat(count)
This function has a single argument.
var str = "hello world"; console.log(str.repeat(5));
Let us see some of the JavaScript methods that are tabulated below:
S.no. | Methods | Description | JavaScript Version |
---|---|---|---|
1. | charAt() | It provides the char value, which is present at the specified index. | ECMAScript1 |
2. | charCodeAt() | It provides the Unicode character value, which is present at the specified index. | ECMAScript1 |
3. | concat() | It provides a combination of two or more strings. | ECMAScript1 |
4. | match() | It is used to search a specified regular expression in a given string and return that expression if a match happens. | ECMAScript1 |
5. | toString() | It gives a string that represents the particular object. | ECMAScript1 |
6. | indexOf() | It gives the position of the char value, which is present in the given string. | ECMAScript1 |
7. | lastIndexOf() | It gives the char value position in the given string by searching a character from the last position. | ECMAScript1 |
8. | replace() | It replaces the given string with the specified replacement. | ECMAScript1 |
9. | search() | It searches a specific regular expression and returns its position if a match occurs. | ECMAScript1 |
10. | valueOf() | It provides the primitive value of the string object. | ECMAScript1 |
11. | slice() | It is used to fetch the part of the given string. | ECMAScript1 |
12. | split() | It splits the string into the array of substring and returns the newly created array. | ECMAScript1 |
13. | substr() | It fetches the part of the given string based on the specified length and starting position. | ECMAScript1 |
14. | substring() | It fetches the part of the given string based on the specified index. | ECMAScript1 |
15. | toLocaleLowerCase() | It converts the given string into the lowercase letter based on the current scale of the host. | ECMAScript1 |
16. | toLocaleUpperCase() | It converts given string into uppercase letters based on the current scale of the host. | ECMAScript1 |
17. | toLowerCase() | It simply converts the given string into the lowercase letter. | ECMAScript1 |
18. | toUpperCase() | It simply converts the given string into the lowercase letter. | ECMAScript1 |
19. | trim() | It trims the white space from the left and right side of the string. | ECMAScript5 |