Code Scroller

Start Scrolling the Code

Home » Javascript String Methods Cheat Sheet

Javascript String Methods Cheat Sheet

Javascript String Methods Cheat Sheet: Use this JavaScript String Methods Cheat Sheet anywhere you need it helps you to remember simple methods very quickly.

The String is an Object in javascript which is a sequence of characters and it can contain letters, numbers, symbols, and whitespace.

Strings are enclosed within either single quotes (”), double quotes (” “), or backticks (` `).it can be empty as well.

So, here I am sharing a few of the JavaScript String Methods Cheat Sheets which will blow your mind.

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

Also, Read

12 Tricks To Write Less JavaScript | JavaScript Tricks

1. String.charAt():

This method in JavaScript is used to return the character at a specified index within a string. It takes the index as a parameter and returns the character from that specific position.

You can use charAt() method in your program or development whenever you want to a single character from a string.

 Example:


let myString='Hello World';
myString.charAt(1); // 'e'

2. String.trim():

This method in JavaScript is used to remove leading and trailing whitespace characters from a string also include spaces, tabs, and line breaks, and returns a new string.

It will help to remove the space, tabs, and line breaks from starting at the beginning and end of the string.

 Example:

let myString="  Hello World  ";
myString.trim(); //"Hello World"

3. String.includes():

It returns true when a string contains a particular part of a string or substring,
or otherwise false.

includes() method will help to check the sub-string or part of the string are present in the string or not.

Example:

let myString="Hello World";
myString.incluedes("World");// true

4.String.indexOf():

String.indexOf() method in JavaScript is used to find the index of the first occurrence of a specified substring within a string or else it returns -1.

It helps you, whenever you need the index of the particular substring within the string. The index starts from zero.

Example:

let myString="Hello World";
myString.indexOf("World"); // 6;

5. String.lastIndexOf():

String.lastIndexOf() method returns the index of the last occurrence of a particular substring within a string. It searches the string from right to left.

Example:

let myString="Hello World";
myString.lastIndexOf("World");// 6

6. String.replace():

String.replace(): method replace occurrences of a specified substring within a string with a
new substring.

It helps whenever you want to change the particular substring with the new string.

Example:

let myString="Hello World";
myString.replace("l","#"); //"He#lo World"

7. String.replaceAll():

String.replaceAll() method replaces all occurrences of a specified substring within a string with
a new substring. It returns a new string after all the replacements.

Example:

let myString="Hello World";
myString.replaceAll("l","#"); //"He##o World

8. String.toUpperCase():

String.toUpperCase(): method in JavaScript is used to convert a string to uppercase. It returns
a new string after converting all the characters into uppercase.

Example:

let myString="Hello World";
myString.toUpperCase(); // "HELLO WORLD"

9. String.toLowerCase():

String.toLowerCase() method in JavaScript is used to convert a string to lowercase. It returns a new string after converting all the characters into lowercase.

Example:

let myString="Hello World";
myString.toLowerCase(); //"hello world"

10. String.startsWith():

String.startsWith() returns true if the string is started with a specified substring otherwise false.

Example:

let myString="Hello World";
myString.startsWith("He"); // true

 

11. String.endsWith():

String.endsWith() returns true if the string ends with a specified substring otherwise false.

Example:

let myString="Hello World";
myString.startsWith("World"); // true

12. String.concat():

String.concat() method is used to concatenate two or more strings together. It returns a new string that is the joining the original string with the given values.

Example:

let myString="Hello";
let newString="World";
myString.concat(newString); // "Hello World"

13. String.slice():

String.slice() method in JavaScript is used to extract a part of a string and return it as a new string.

Example:

let myString="Hello World";
myString.slice(6); //"World"

14. String.split():

String.split() method in JavaScript is used to split a string into an array of substrings based on a specified separator.

Example:

 
let myString="Hello World";
myString.split(" "); // ["Hello","World"]

15. String.repeat():

String.repeat() method returns a new string consisting of the original string repeated with a specified number of copies of string.

Example:

 
let myString="Hello";
myString.repeat(3); "HelloHelloHello"

FAQs : Javascript String Methods Cheat Sheet

Q) What is a JavaScript string?

In JavaScript, a string is a sequence of characters enclosed within single quotes (”) or double quotes (” “). It represents textual data and can be manipulated using various string methods.

Q) What are the string methods in JavaScript?

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

Q) How can I get the length of a string in JavaScript?

You can use the length property to retrieve the number of characters in a string. For example,

let str = "Hello"; 
console.log(str.length); // 5

Q) How can I access individual characters in a string?

You can access individual characters in a string using bracket notation []. Each character in the string has an index starting from 0. For example,

let str = "Hello"; 
console.log(str[0]); // output "H".

Q) How can I split a string into an array in JavaScript?

The split() method allows you to split a string into an array of substrings based on a specified delimiter. For example,

 
let str = "Hello,World"; 
console.log(str.split(",")); 
// output ["Hello", "World"].

Conclusion:

So I have shared here JavaScript String Methods Cheat Sheets. These methods allow you to perform operations such as concatenation, extraction of substrings, case transformations, splitting strings, and more.

Hope you enjoyed it and if you have any queries you can ask through comments.Thank You

Aishwarya Raj

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top