Back to JavaScript Cheat Sheet

String Methods in JavaScript - The Ultimate Cheat Sheet

Looking for a quick guide on the fundamentals of strings in JavaScript? This cheat sheet has got you covered!
6 min readLogan FordLogan Ford
What are strings in JS?

String Methods in JavaScript

Understanding strings in JavaScript is crucial for any developer. In this cheat sheet, we'll cover the most important string methods in JavaScript.


If you're looking to understand what strings are, check out what is a String in JS?.


Getting the length of a string in JavaScript

To get the length of a string in JavaScript, you can use the length property. This will return how many characters are present in a given string.


1const message = "Hello, world!";
2const length = message.length; // 13
3
4// output: 13

Converting a string to uppercase in JavaScript

To convert a string to uppercase in JavaScript, you can use the toUpperCase method. This will return a new string with all the characters converted to uppercase.


1const message = "Hello, world!";
2const uppercaseMessage = message.toUpperCase();
3
4// output: "HELLO, WORLD!"

Converting a string to lowercase in JavaScript

To convert a string to lowercase in JavaScript, you can use the toLowerCase method. This will return a new string with all the characters converted to lowercase.


1const message = "Hello, world!";
2const lowercaseMessage = message.toLowerCase();
3
4// output: "hello, world!"

Checking if a string contains a substring in JavaScript

To check if a string contains a substring in JavaScript, you can use the includes method. This will return true if the substring is present in the string and false otherwise.


1const message = "Hello, world!";
2const containsHello = message.includes("Hello");
3
4// output: true

Finding the index of a substring in JavaScript

To find the index of a substring in JavaScript, you can use the indexOf method. This will return the index of the first occurrence of the substring in the string.


1const message = "Hello, world!";
2const indexOfHello = message.indexOf("Hello");
3
4// output: 0

Replacing a substring in JavaScript

To replace a substring in JavaScript, you can use the replace method. This will return a new string with the substring replaced with the new substring.


1const message = "Hello, world!";
2const replacedMessage = message.replace("world", "everyone");
3
4// output: "Hello, everyone!"

Extracting a substring in JavaScript

To extract a substring in JavaScript, you can use the substring method. This will return a new string with the substring extracted from the string.


1const message = "Hello, world!";
2const substring = message.substring(0, 5);
3
4// output: "Hello"

Removing whitespace from a string in JavaScript

To remove whitespace from a string in JavaScript, you can use the trim method. This will return a new string with the whitespace removed from the beginning and end of the string.


1const message = " Hello, world! ";
2const trimmedMessage = message.trim();
3
4// output: "Hello, world!"

Splitting a string in JavaScript

To split a string in JavaScript, you can use the split method. This will return an array of substrings from the string.


1const message = "Hello, world!";
2const splitMessage = message.split(",");
3
4// output: ["Hello", " world!"]

Joining an array of strings into a single string in JavaScript

To join an array of strings into a single string in JavaScript, you can use the join method. This will return a new string with the array of strings joined together.


1const message = "Hello, world!";
2const joinedMessage = message.join(",");
3
4// output: "Hello, world!"

Converting a string to an array in JavaScript

To convert a string to an array in JavaScript, you can use the split method. This will return an array of substrings from the string.


1const message = "Hello, world!";
2const splitMessage = message.split(",");
3
4// output: ["Hello", " world!"]

Converting an array to a string in JavaScript

To convert an array to a string in JavaScript, you can use the join method. This will return a new string with the array of strings joined together.


1const message = "Hello, world!";
2const joinedMessage = message.join(",");
3
4// output: "Hello, world!"

Repeating a string in JavaScript

To repeat a string in JavaScript, you can use the repeat method. This will return a new string with the string repeated the number of times specified.


1const message = "Hello, world!";
2const repeatedMessage = message.repeat(3);
3
4// output: "Hello, world!Hello, world!Hello, world!"

Find if a string ends with a substring in JavaScript

To find if a string ends with a substring in JavaScript, you can use the endsWith method. This will return true if the string ends with the substring and false otherwise.


1const message = "Hello, world!";
2const endsWithWorld = message.endsWith("world");
3
4// output: true

Find if a string starts with a substring in JavaScript

To find if a string starts with a substring in JavaScript, you can use the startsWith method. This will return true if the string starts with the substring and false otherwise.


1const message = "Hello, world!";
2const startsWithHello = message.startsWith("Hello");
3
4// output: true

Looking to learn through real examples?

Looking to master string methods in JS? Try these coding challenges to reinforce your knowledge!

Accelerate Your JavaScript Journey

Ready to master JavaScript strings and beyond? TechBlitz offers comprehensive JavaScript training with hands-on exercises, real-world projects, and expert guidance. Whether you're just starting out or looking to deepen your understanding of JavaScript concepts, our platform provides the resources you need to succeed. Join our community of developers and take your programming skills to the next level!

Land your dream tech job faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article