GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • JavaScript String constructor Property
  • JavaScript Strings Tutorial

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript String Methods - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript String Methods

Last Updated : 1 Jun, 2026

JavaScript provides a variety of built-in methods to work with strings — allowing you to extract, modify, search, and format text with ease. Below are some of the most commonly used core string methods:

slice()

slice() extracts a part of the string based on the given stating-index and ending-index and returns a new string.

JavaScript
// Define a string variable 
let A = 'Geeks for Geeks';

// Use the slice() method to extract a substring
let b = A.slice(0, 5);
let c = A.slice(6, 9);
let d = A.slice(10);

// Output the value of variable
console.log(b);
console.log(c);
console.log(d);

substring()

substring() returns the part of the given string from the start index to the end index. Indexing starts from zero (0).

JavaScript
// Define a string variable
let str = "Mind, Power, Soul";

// Use the substring() method to extract a substring 
let part = str.substring(6, 11);

// Output the value of variable
console.log(part);

substr()

substr() This method returns the specified number of characters from the specified index from the given string. It extracts a part of the original string.

JavaScript
// Define a string variable 'str'
let str = "Mind, Power, Soul";

// Use the substr() method to extract a substring f
let part = str.substr(6, 5);

// Output the value of variable
console.log(part);

replace()

replace() replaces a part of the given string with another string or a regular expression. The original string will remain unchanged.

JavaScript
// Define a string variable 'str' 
let str = "Mind, Power, Soul";

// Use the replace() method to replace the substring
let part = str.replace("Power", "Space");

// Output the resulting string after replacement
console.log(part);

replaceAll()

replaceAll() 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
// Define a string variable 'str'
let str = "Mind, Power, Power, Soul";

// Use the replaceAll() method to replace all occurrences
//of "Power" with "Space" in the string 'str'
let part = str.replaceAll("Power", "Space");

// Output the resulting string after replacement
console.log(part);

toUpperCase()

toUpperCase() 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.

JavaScript
// Define a string variable
let gfg = 'GFG ';

// Define another string variable 
let geeks = 'stands-for-GeeksforGeeks';

// Convert the string 'geeks' to uppercase using the toUpperCase() method
console.log(geeks.toUpperCase());

toLowerCase()

toLowerCase() converts all the characters present in the so lowercase and returns a new string with all the characters in lowercase.

JavaScript
// Define a string variable
let gfg = 'GFG ';

// Define a string variable 
let geeks = 'stands-for-GeeksforGeeks';

// Convert the string 'geeks' to lowercase using the toLowerCase() method
console.log(geeks.toLowerCase());

concat()

concat() combines the text of two strings and returns a new combined or joined string. To concatenate two strings, we use the 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.

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));

trim()

trim() 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.

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)

trimStart()

trimStart() 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
// Define a string variable 
let str = "  Soul";

// Output the original value of the string 
console.log(str);

// Use the trimStart() method to remove leading whitespace from the string 'str'
let part = str.trimStart();

// Output the resulting string after removing leading whitespace
console.log(part);

trimEnd()

trimEnd() 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
// Define a string variable 
let str = "Soul  ";

// Output the original value of the string 'str'
console.log(str);

// Use the trimEnd() method to remove trailing whitespace from the string 'str'
let part = str.trimEnd();

// Output the resulting string after removing trailing whitespace
console.log(part);

padStart()

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

JavaScript
// Define a string variable 
let stone = "Soul";

// Use the padStart() method to add padding characters "Mind "
//to the beginning of the string 'stone' 
stone = stone.padStart(9, "Mind ");

// Output the resulting string after padding
console.log(stone);

padEnd()

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

JavaScript
// Define a string variable 
let stone = "Soul";

// Use the padEnd() method to add padding characters
//" Power" to the end of the string 'stone' 
stone = stone.padEnd(10, " Power");

// Output the resulting string after padding
console.log(stone);

charAt()

charAt() returns the character at the specified index. String in JavaScript has zero-based indexing.

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));

charCodeAt()

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

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));

split()

split() 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.

JavaScript
let gfg = 'GFG '
let geeks = 'stands-for-GeeksforGeeks'

// Split string on '-'. 
console.log(geeks.split('-'))

More JS String Methods

Below is the JavaScript string functions list:

Instance Methods

Description

at()Find the character at the specified index.
anchor()Creates an anchor element that is used as a hypertext target.
charAt()Returns that character at the given index of the string.
charCodeAt()Returns a Unicode character set code unit of the character present at the index in the string.
codePointAt()Return a non-negative integer value i.e, the code point value of the specified element.
concat()Join two or more strings together in JavaScript.
endsWith()Whether the given string ends with the characters of the specified string or not.
includes()Returns true if the string contains the characters, otherwise, it returns false.
indexOf()Finds the index of the first occurrence of the argument string in the given string.
lastIndexOf()Finds the index of the last occurrence of the argument string in the given string.
localeCompare()Compare any two elements and returns a positive number
match()Search a string for a match against any regular expression.
matchAll()Return all the iterators matching the reference string against a regular expression.
normalize()Return a Unicode normalization form of a given input string.
padEnd()Pad a string with another string until it reaches the given length from rightend.
padStart()Pad a string with another string until it reaches the given length from leftend.
repeat()Build a new string containing a specified number of copies of the string.
replace()Replace a part of the given string with some another string or a regular expression
replaceAll()Returns a new string after replacing all the matches of a string with a specified string/regex.
search()Search for a match in between regular expressions and a given string object.
slice()Return a part or slice of the given input string.
split()Separate given string into substrings using a specified separator provided in the argument.
startsWith()Check whether the given string starts with the characters of the specified string or not.
substr()Returns the specified number of characters from the specified index from the given string.
substring()Return the part of the given string from the start index to the end index.
toLowerCase()Converts the entire string to lowercase.
toLocaleLowerCase()Returns the calling string value converted to a lowercase letter.
toLocaleUpperCase()Returns the calling string value converted to a uppercase letter.
toUpperCase()Converts the entire string to uppercase.
toString()Return the given string itself.
trim()Remove the white spaces from both ends of the given string.
trimEnd()Remove white space from the end of a string.
trimStart()Remove white space from the start of a string.
valueOf()Return the value of the given string.
string[Symbol.iterator]()This method is used to make String iterable. [@@iterator]() returns an iterator object which iterates over all code points of the String.
fromCharCode(n1, n2, ..., nX)This method is used to create a string from the given sequence of UTF-16 code units. This method returns a string, not a string object.
fromCodePoint(a1, a2, a3, ....)This method in JavaScript that is used to return a string or an element for the given sequence of code point values (ASCII value).
isWellFormed()This method is used to check if the string contains a lone surrogate or not
String.raw(str, ...sub)This is a static method that is used to get the raw string form of template literals. These strings do not process escape characters.
toWellFormed()This method is used to return where all lone surrogates of this string are replaced with the Unicode replacement character.

Also Check

  • JavaScript String constructor Property
  • JavaScript Strings Tutorial
  • JavaScript String length Property
  • JavaScript String Programming Examples
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.