Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String Methods

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Strings are texts that help in holding data that can be represented. A JavaScript string stores a series of characters like “GeeksforGeeks”. A string can be any text inside double or single quotes.

JavaScript String Methods and Property: String methods and properties are important to perform any operation on the given string, the string search methods have a separate article for better understanding. Below we have described a few important methods with the proper example code.

   

String indexes start from 0. The first character is in position 0 and the second in 1 and the same follows. We can call any of the pre-defined methods of JavaScript as it automatically converts from string primitive to objects.

Example:

 let gfg= "geeksforgeeks";

JavaScript String length Property: This property returns the number of characters present in the string. In the case of an array, this property returns the number of elements present in the array.

Example: This example describes the JavaScript String Length property.

JavaScript




<script>
    // JavaScript to illustrate length property   
    function func() {
         
        // length property for string
        console.log("GFG".length)
    }
    func();
</script>


Output:

3

JavaScript slice(startIndex, endIndex) Method: This method extracts a part of the string based on the given stating-index and ending-index and returns a new string.

Example: This example describes the JavaScript String slice() method.

javascript




let A = 'Geeks for Geeks';
b = A.slice(0,5);
c = A.slice(6,9);
d = A.slice(10);
 
console.log(b);
console.log(c);
console.log(d);


Output:

Geeks
for
Geeks

JavaScript substring(startIndex, endIndex) Method: This method returns the part of the given string from the start index to the end index. Indexing start from zero (0).

Javascript




let str = "Mind, Power, Soul";
let part = str.substring(6, 11);
console.log(part);


Output:

Power

JavaScript substr(start, length) Method: This method returns the specified number of characters from the specified index from the given string. It basically extracts a part of the original string.

Javascript




let str = "Mind, Power, Soul";
let part = str.substr(6, 5);
console.log(part);


Power

JavaScript replace(replaceValue, replaceWithValue) Method: This method replaces a part of the given string with another string or a regular expression. The original string will remain unchanged.

Javascript




let str = "Mind, Power, Soul";
let part = str.replace("Power", "Space");
console.log(part);


Output:

Mind, Space, Soul

JavaScript replaceAll(regexp | substr , newSubstr | function) Method: This method returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

Javascript




let str = "Mind, Power, Power,  Soul";
let part = str.replaceAll("Power", "Space");
console.log(part);


Output:

Mind, Space, Space,  Soul

JavaScript toUpperCase(stringVariable) Method: This method converts all the characters present in the String to upper case and returns a new String with all characters in upper case. This method accepts single parameter stringVariable string that you want to convert in upper case.

Example: This example describes the JavaScript String toUpperCase() method.

javascript




let gfg = 'GFG '
let geeks = 'stands-for-GeeksforGeeks';
 
console.log(geeks.toUpperCase(geeks)) ;


Output:

STANDS-FOR-GEEKSFORGEEKS

JavaScript toLowerCase(stringVariable) Method: This method converts all the characters present in the so lowercase and returns a new string with all the characters in lowercase.

tring t

Example: This example describes the JavaScript String toLowerCase() method.

javascript




let gfg = 'GFG ';
let geeks = 'stands-for-GeeksforGeeks';
 
console.log(geeks.toLowerCase(geeks));


Output:

stands-for-geeksforgeeks

JavaScript concat(objectOfString) Method: This method combines the text of two strings and returns a new combined or joined string. To concatenate two strings, we use concat() method on one object of string and send another object of string as a parameter. This method accepts one argument. The variable contains text in double quotes or single quotes.

Example: This example describes the JavaScript String concat() method.

javascript




let gfg = 'GFG ';
let geeks = 'stands for GeeksforGeeks';
 
// Accessing concat method on an object
// of String passing another object
// as a parameter
console.log(gfg.concat(geeks));


Output:

GFG stands for GeeksforGeeks

JavaScript trim() Method: This method is used to remove either white spaces from the given string. This method returns a new string with removed white spaces. This method is called on a String object. This method doesn’t accept any parameter.

Example: This example describes the JavaScript String trim() method.

javascript




let gfg = 'GFG    ';
let geeks = 'stands-for-GeeksforGeeks';
 
// Storing new object of string
// with removed white spaces
let newGfg = gfg.trim();
 
// Old length
console.log(gfg.length);
 
// New length
console.log(newGfg.length)


Output:

7
3

JavaScript trimStart() Method: This method removes whitespace from the beginning of a string. The value of the string is not modified in any manner, including any whitespace present after the string.

Javascript




let str = "  Soul";
console.log(str)
let part = str.trimStart();
console.log(part);


Output:

  Soul
Soul

JavaScript trimEnd() Method: This method removes white space from the end of a string. The value of the string is not modified in any manner, including any white-space present before the string.

Javascript




let str = "Soul  ";
console.log(str)
let part = str.trimEnd();
console.log(part);


Output:

Soul    
Soul

JavaScript padStart() Method: This method pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.

Javascript




let stone = "Soul";
stone = stone.padStart(9,"Mind ");
console.log(stone);


Output:

Mind Soul

JavaScript padEnd() Method: This method pad a string with another string until it reaches the given length. The padding is applied from the right end of the string.

Javascript




let stone = "Soul";
stone = stone.padEnd(10," Power");
console.log(stone);


Output:

Soul Power

JavaScript charAt(indexOfCharacter) Method: This method returns the character at the specified index. String in JavaScript has zero-based indexing.

Example: This example describes the JavaScript string charAt() method.

javascript




let gfg = 'GeeksforGeeks';
let geeks = 'GfG is the best platform to learn and\n'+
'experience Computer Science.';
 
// Print the string as it is
console.log(gfg);
 
console.log(geeks);
 
// As string index starts from zero
// It will return first character of string
console.log(gfg.charAt(0));
 
console.log(geeks.charAt(5));


Output:

GeeksforGeeks
GfG is the best platform to learn 
and experience Computer Science.
G
s

JavaScript charCodeAt(indexOfCharacter) Method: This method returns a number that represents the Unicode value of the character at the specified index. This method accepts one argument.

Example: This example describes the JavaScript String charCodeAt() Method.

javascript




let gfg = 'GeeksforGeeks';
let geeks = 'GfG is the best platform\n\
to learn and experience\n\
Computer Science.';
 
// Return a number indicating Unicode
// value of character at index 0 ('G')
console.log(gfg.charCodeAt(0));
console.log(geeks.charCodeAt(5));


Output:

71
115

JavaScript split(character) Method: This method splits the string into an array of sub-strings. This method returns an array. This method accepts a single parameter character on which you want to split the string.

Example: This example describes the JavaScript String split() method.

javascript




let gfg = 'GFG '
let geeks = 'stands-for-GeeksforGeeks'
 
// Split string on '-'.
console.log(geeks.split('-'))


Output:

['stands', 'for', 'GeeksforGeeks']

My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials