AI Hints
Home›JavaScript Tutorial›Text and Truth
JavaScript · Lesson 11 of 138

String Methods

Length, toUpperCase, includes.

⏱ 2 min read▶ Video at 11:49🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Strings come with lots of built in tools, called methods. You call a method with a dot, the method's name, and brackets. Let's meet the ones you'll use every day.

String Methods
From the video: String Methods
  • Built-in tools
  • string.method()
  • The everyday ones

Everyday methods

To upper case and to lower case change the letters. Trim removes spaces from both ends, which is great for tidying what users type. Includes checks whether some text is inside. And replace swaps one piece of text for another. Run it.

app.jsjavascript
const text = "  Hello, World  ";

console.log(text.trim());
console.log(text.toUpperCase());
console.log(text.toLowerCase().trim());
console.log(text.includes("World"));
console.log(text.trim().replace("World", "JavaScript"));
Output
Hello, World
  HELLO, WORLD
hello, world
true
Hello, JavaScript

Slicing and splitting

Slice copies part of a string, from a start position up to, but not including, an end position. Split breaks a string into a list of pieces, wherever it finds a separator. Run it. Strings themselves never change. Methods always give you a new string.

app.jsjavascript
const email = "ana@example.com";

console.log(email.slice(0, 3));
console.log(email.split("@"));
console.log(email.indexOf("@"));
console.log("original:", email);
Output
ana
[ 'ana', 'example.com' ]
3
original: ana@example.com

Summary

String methods tidy, search, and cut text. They never change the original, they return a new string. Next, true and false values.

Key takeaways
  • trim, toUpperCase
  • includes, replace
  • slice, split
Next up: Booleans — true and false