JavaScript 21 String Methods Cheat Sheet 2021

·

JavaScript 21 String Methods Cheat Sheet 2021

Hello guy’s welcome back to my blog. Today in this blog post, I am going to tell you, JavaScript 21 String Methods Cheat Sheet 2021.

JavaScript 21 String Methods Cheat Sheet 2021
JavaScript 21 String Methods Cheat Sheet 2021

1 .length – Finding the length of the string:

var str = 'this string has 29 characters';

console.log(str.length);

//29

2 .trim() – Removing white space:

var str = '    this string has 29 characters';

console.log(str.trim());

//this string has 29 characters

3 .includes() – Check if string contains substring:

var str = 'Jassa therichpost';

var str2 = 'Jassa';

var str3 = 'Ajay';

console.log(str.includes(str2));
//true

console.log(str.includes(str3));
//false

4 .indexOf() — Find the index of a substring:

var str = 'Jassa therichpost';

var str2 = 'Jassa';

var str3 = 'Ajay';

console.log(str.indexOf(str2));

//0

console.log(str.indexOf(str3));

//-1

5 .toUpperCase() — Capitalizes entire string:

var str = 'jassa therichpost';

console.log(str.toUpperCase());

//JASSA THERICHPOST

6 .toLowerCase() — Lower cases entire string:

var str = 'JaSsa therichPost';

console.log(str.toLowerCase());

//jassa therichpost

7 .replace() — Replaces strings with new values:

var str = 'Jassa therichpost';

var str1 = 'Jassa';

var str2 = 'Ajay';

console.log(str.replace(str1, str2));

//Ajay therichpost

8 .slice() — Return a section of a string:

var str = 'Jassa therichpost';

console.log(str.slice(2));

//ssa therichpost

9 .split() — Converts string into an array of strings:

var str = 'Jassa therichpost';

console.log(str.split(" "));

//["Jassa", "therichpost]

10 .repeat() — Repeats a string a specified number of times:

var str = 'Jassa therichpost. ';

console.log(str.repeat(2));

//Jassa therichpost. Jassa therichpost.

11 .match() — Returns array of matching strings:

var str = 'Jassa therichpost';

var str1 = 'Jassa';

console.log(str.match(str1));

//["Jassa"]

12 .charAt() — Returns the character at an index:

var str = 'Jassa therichpost';
  
console.log(str.charAt(2));

//s

13 .charCodeAt() —Return the unicode at an index:

var str = 'Jassa therichpost';
  
console.log(str.charCodeAt(2));

//115

14 .concat() – Adding the two strings:

var str = 'Jassa therichpost ';

var str1 = 'Ajay';

console.log(str.concat(str1));

//Jassa therichpost Ajay

15 .search() – Find the string:

var str = 'Jassa therichpost ';

var str1 = 'Jassa';

console.log(str.search(str1));

//0 means true

//if it will -1 then string not there

16 .substring() – Extracts the characters from a string:

var str = 'Jassa therichpost ';
  
console.log(str.substring(8, 11));

//eri

17 .substr()- Extracts parts of a string:

var str = 'Jassa therichpost ';
  
console.log(str.substr(8, 11));

//erichpost

18 .valueOf() – Return the primitive value of the string:

var str = 'Jassa therichpost ';
  
console.log(str.valueOf());

//Jassa therichpost

19 .startsWith() – Check if a string starts with:

var str = 'Jassa therichpost ';
  
console.log(str.startsWith('Jassa'));

//true

20 .endsWith() – Check if a sting ends with:

var str = 'Jassa therichpost';
  
console.log(str.endsWith('therichpost'));

//true

21 .lastIndexOf() – returns the position of the last occurrence of a specified value in a string:

var str = 'Jassa therichpost';
  
console.log(str.lastIndexOf('therichpost'));

//6

This is it guy’s. I will come with more post which will helpful to all. If you have any kind of query then please comment below.

Jassa

Thanks.

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.