TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 5 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript String Methods: slice, includes, split and More

Strings hold text. You can write them with single quotes, double quotes or backticks; the first two are identical, and backticks add extra features covered in the next chapter. Strings are immutable: every method returns a new string and leaves the original unchanged.

Length and characters

word.length is the number of characters (more precisely, UTF-16 code units, so some emoji count as two). Square brackets read one character, starting at 0: word[0]. The at() method also accepts negative positions counting from the end, so word.at(-1) is the last character; it is available in all current browsers and Node.js 16.6 and later.

Extracting parts

slice(start, end) returns the characters from start up to, but not including, end. Negative numbers count from the end, so word.slice(-6) gives the last six characters. There is an older substring() method with slightly different rules for negative values; slice is the one to remember.

JavaScript · editable

Expected output

10 J t
Java Script
JAVASCRIPT javascript
true 1 true
trim me|
["a", "b", "c"]
hahaha 007
one 2 2

Searching

  • includes(text) returns true or false.
  • indexOf(text) returns the position of the first match, or -1 if there is none.
  • startsWith() and endsWith() check the edges.

All of these are case-sensitive. To compare without case, lowercase both sides first with toLowerCase().

Transforming

  • toUpperCase() and toLowerCase() change case.
  • trim() removes whitespace at both ends; trimStart() and trimEnd() handle one side.
  • split(separator) returns an array of pieces, and the array method join() reverses it.
  • repeat(n) repeats the string.
  • padStart(length, fill) and padEnd() pad to a fixed width, handy for zero-padded numbers like 007.
  • replace(a, b) replaces the first match only, while replaceAll(a, b) replaces every match.

Because each method returns a string, you can chain them: input.trim().toLowerCase().

Common mistakes

  • Expecting replace("a", "b") to change every occurrence; use replaceAll.
  • Testing if (text.indexOf("x")). A match at position 0 is falsy and -1 is truthy. Use includes().
  • Calling a method and forgetting to store the result.

Summary

  • Strings are immutable; methods return new strings.
  • slice() extracts, includes() and indexOf() search, split() breaks into an array.
  • replace changes the first match; replaceAll changes all of them.

Quiz: Strings

1. What is "JavaScript".slice(0, 4)?

slice includes the start index and stops before the end index.

2. What does "a-b-a".replace("a", "x") return?

replace with a string pattern changes only the first match.

3. What is "cat".indexOf("z")?

indexOf returns -1 when the text is not found.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.