Search⌘ K
AI Features

Additional String Methods

Discover how to use new ES6 string methods including startsWith, endsWith, includes, and repeat to manipulate strings effectively in JavaScript. Understand their syntax and practical uses to improve your code.

There are many methods that we can use against strings. Here’s a list of a few of them:

  1. indexOf()

Gets the position of the first occurrence of the specified value in a string.

Javascript (babel-node)
const str = "this is a short sentence";
console.log(str.indexOf("short"));
// Output: 10
  1. slice()

Pulls a specified part of a string as a new string.

Javascript (babel-node)
const str = "pizza, orange, cereals"
console.log(str.slice(0, 5));
// Output: "pizza"
  1. toUpperCase()

Turns all characters of a string to uppercase.

Javascript (babel-node)
const str = "i ate an apple"
console.log(str.toUpperCase());
// Output: "I ATE AN APPLE"
  1. toLowerCase()
...