JavaScript String Methods With All Complete Examples

In this tutorial we will learn all methods of JavaScript String. JavaScript is commonly used and very popular programming language. It can be used for various purposes like backend and & frontend as well as. Please see below all the string methods in js.

javascript string methods
javascript string methods

Here are all the String methods in JavaScript

There are many methods in JavaScript for String. We tried to cover all those as below.

  1. charAt()
  2. charCodeAt()
  3. concat()
  4. indexOf()
  5. lastIndexOf()
  6. substring()
  7. slice()
  8. toUpperCase()
  9. toLowerCase()
  10. replace()
  11. trim()
  12. split()
  13. startsWith()
  14. endsWith()
  15. includes()

Examples

Below are some explanation with examples.

1. length Method:

The length method returns the length of a string.

Example:

let str = "Hello, World!";
let length = str.length;
console.log(length); // Output: 13

2. charAt() Method:

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

Example:

let str = "JavaScript";
let char = str.charAt(2);
console.log(char); // Output: v

3. concat() Method:

The concat() method concatenates two or more strings.

Example:

let str1 = "Hello, ";
let str2 = "World!";
let result = str1.concat(str2);
console.log(result); // Output: Hello, World!

4. indexOf() Method:

The indexOf() method returns the index of the first occurrence of a specified value in a string.

Example:

let str = "Hello, World!";
let index = str.indexOf("World");
console.log(index); // Output: 7

5. substring() Method:

The substring() method extracts characters from a string between two specified indices.

Example:

let str = "JavaScript";
let substr = str.substring(2, 5);
console.log(substr); // Output: vaS

6. toUpperCase() Method:

The toUpperCase() method converts all characters in a string to uppercase.

Example:

let str = "JavaScript";
let uppercaseStr = str.toUpperCase();
console.log(uppercaseStr); // Output: JAVASCRIPT

7. toLowerCase() Method:

The toLowerCase() method converts all characters in a string to lowercase.

Example:

let str = "JavaScript";
let lowercaseStr = str.toLowerCase();
console.log(lowercaseStr); // Output: javascript

More JavaScript String Methods

8. replace() Method:

The replace() method replaces a specified substring or pattern with another string.

Example:

let str = "Hello, World!";
let newStr = str.replace("World", "Universe");
console.log(newStr); // Output: Hello, Universe!

9. split() Method:

The split() method splits a string into an array of substrings based on a specified delimiter.

Example:

let str = "apple,orange,banana";
let fruits = str.split(",");
console.log(fruits); // Output: ["apple", "orange", "banana"]

10. trim() Method:

The trim() method removes whitespace from both ends of a string.

Example:

let str = "   Hello, World!   ";
let trimmedStr = str.trim();
console.log(trimmedStr); // Output: Hello, World!

11. charAt() Method:

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

Example:

let str = "JavaScript";
let char = str.charAt(2);
console.log(char); // Output: v

12. charCodeAt() Method:

The charCodeAt() method returns the Unicode value of the character at a specified index.

Example:

let str = "JavaScript";
let unicodeValue = str.charCodeAt(2);
console.log(unicodeValue); // Output: 118

13. endsWith() Method:

The endsWith() method checks if a string ends with a specified substring.

Example:

let str = "Hello, World!";
let endsWithWorld = str.endsWith("World!");
console.log(endsWithWorld); // Output: true

14. startsWith() Method:

The startsWith() method checks if a string starts with a specified substring.

Example:

let str = "Hello, World!";
let startsWithHello = str.startsWith("Hello");
console.log(startsWithHello); // Output: true

15. includes() Method:

The includes() method checks if a string contains a specified substring.

Example:

let str = "Hello, World!";
let includesWorld = str.includes("World");
console.log(includesWorld); // Output: true

These are some basic Js string methods with examples. It’s important to note that JavaScript provides a rich set of string methods, and the examples here are just a starting point.

Thank you for reaching out us. See JavaScript Array Filter With some examples. You can see on W3School also.

Don’t miss new tips!

We don’t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment

Translate »
Scroll to Top