Basic String Methods

Javascript strings are primitive and immutable: All string methods produces a new string without altering the original string.

String length
String charAt()
String charCodeAt()
String at()
String [ ]
String slice()
String substring()
String substr()

See Also:

String Search Methods
String Templates

String toUpperCase()
String toLowerCase()
String concat()
String trim()
String trimStart()
String trimEnd()
String padStart()
String padEnd()
String repeat()
String replace()
String replaceAll()
String split()


JavaScript String Length

The length property returns the length of a string:

Example

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

 


Extracting String Characters

There are 4 methods for extracting string characters:

  • The at(position) Method
  • The charAt(position) Method
  • The charCodeAt(position) Method
  • Using property access [] like in arrays

JavaScript String charAt()

The charAt() method returns the character at a specified index (position) in a string:

Example

let text = "HELLO WORLD";
let char = text.charAt(0);

 


JavaScript String charCodeAt()

The charCodeAt() method returns the code of the character at a specified index in a string:

The method returns a UTF-16 code (an integer between 0 and 65535).

Example

let text = "HELLO WORLD";
let char = text.charCodeAt(0);

 


JavaScript String at()

ES2022 introduced the string method at():

Examples

Get the third letter of name:

const name = "W3Schools";
let letter = name.at(2);

 

Get the third letter of name:

const name = "W3Schools";
let letter = name[2];

 

The at() method returns the character at a specified index (position) in a string.

The at() method is supported in all modern browsers since March 2022:

Note

The at() method is a new addition to JavaScript.

It allows the use of negative indexes while charAt() do not.

Now you can use myString.at(-2) instead of charAt(myString.length-2).

Browser Support

at() is an ES2022 feature.

JavaScript 2022 (ES2022) is supported in all modern browsers since March 2023:

         

Chrome 94

Edge 94

Firefox 93

Safari 16.4

Opera 79

Sep 2021

Sep 2021

Oct 2021

Mar 2023

Oct 2021


Property Access [ ]

Example

let text = "HELLO WORLD";
let char = text[0];

 

Note

Property access might be a little unpredictable:

·         It makes strings look like arrays (but they are not)

·         If no character is found, [ ] returns undefined, while charAt() returns an empty string.

·         It is read only. str[0] = "A" gives no error (but does not work!)

Example

let text = "HELLO WORLD";
text[
0] = "A";    // Gives no error, but does not work

 


Extracting String Parts

There are 3 methods for extracting a part of a string:

  • slice(startend)
  • substring(startend)
  • substr(startlength)

JavaScript String slice()

slice() extracts a part of a string and returns the extracted part in a new string.

The method takes 2 parameters: start position, and end position (end not included).

Example

Slice out a portion of a string from position 7 to position 13:

let text = "Apple, Banana, Kiwi";
let part = text.slice(713);

 

Note

Login
ADS CODE