The JavaScript string is a character sequence that is represented as an object. A string can be any combination of letters, integers, special characters, and arithmetic values.
Ways to create string
Double quotes are used to build the string literal. Lets take an example
var val="Example string literal";
console.log(val); // Example string literal
We use new keyword for creating string object
var val = new String("Example string object");
console.log(val); // Example string object
Method | Description | Example |
---|---|---|
charAt() | It returns the char value found at the given index. |
|
concat() | This method is used to combine two or more arrays. |
|
indexOf() | This method returns either -1 if an element cannot be found or the first index in the array at which it can be located. |
|
lastIndexOf() | This method returns either -1 if an element cannot be found or the last index in the array at which it can be located. |
|
toLowerCase() | Converts strings into lowercase. |
|
toUpperCase() | Converts strings into uppercase. |
|
slice(startIndex, endIndex) | Used to extract specific portions of strings without changing the original strings |
|
trim() | Eliminates whitespace from strings both ends and produces a new string. |
|
split() | This method creates a list of substrings from a string and returns them in the form of an array. |
|
replace() | Finds the first instance of a string and replaces it with the provided string. |
|
substr() | The portion of the string between the start and end indexes is returned |
|
valueOf() | It returns primitive value. |
|