JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Strings

javascript
junior
strings

What are the most commonly used string methods in JavaScript?

strings
methods
template-literals
fundamentals
Quick Answer

Key string methods include: length, indexOf/includes for searching, slice/substring for extracting, toUpperCase/toLowerCase for case, trim for whitespace, split for converting to arrays, replace/replaceAll for substitution, and template literals for interpolation.

Detailed Explanation

Strings in JavaScript are immutable — all methods return a new string.

Searching:

  • includes(str) — returns boolean
  • indexOf(str) — returns position or -1
  • startsWith(str) / endsWith(str)

Extracting:

  • slice(start, end) — extracts a section (supports negative indices)
  • substring(start, end) — similar but no negative indices
  • charAt(index) or bracket notation str[0]

Transforming:

  • toUpperCase() / toLowerCase()
  • trim() / trimStart() / trimEnd()
  • padStart(len, char) / padEnd(len, char)
  • repeat(count)

Replacing:

  • replace(search, replacement) — first occurrence
  • replaceAll(search, replacement) — all occurrences

Splitting/Joining:

  • split(separator) — string → array
  • Array.join(separator) — array → string

Template Literals:

  • Backticks with ${expression} for interpolation
  • Support multi-line strings

Code Examples

Common string operationsJavaScript
const str = '  Hello, World!  ';

// Searching
str.includes('World');    // true
str.indexOf('World');     // 9
str.startsWith('  He');   // true

// Extracting
str.trim();               // 'Hello, World!'
str.slice(2, 7);          // 'Hello'
str.trim().slice(-6);     // 'World!'

// Transforming
'hello'.toUpperCase();    // 'HELLO'
'5'.padStart(3, '0');     // '005'

// Replacing
'aabbcc'.replace('b', 'x');    // 'aaxbcc'
'aabbcc'.replaceAll('b', 'x'); // 'aaxxcc'

// Split and join
'a,b,c'.split(',');       // ['a', 'b', 'c']
['a', 'b', 'c'].join('-'); // 'a-b-c'

Real-World Applications

Use Cases

URL Slug Generation

Converting titles to URL-friendly slugs using toLowerCase, replace, and trim

Search and Filter

Using includes and indexOf for real-time search filtering of lists as users type

Input Sanitization

Trimming whitespace and normalizing user input before processing or submitting to an API

Mini Projects

Slug Generator

beginner

Build a utility that converts any string to a URL-friendly slug with proper character handling

Text Formatter

intermediate

Build a tool with find/replace, case conversion, word counting, and character statistics

Industry Examples

validator.js

String validation library providing isEmail(), isURL(), isIP() and many other validators for Node.js and browsers

Lodash

String utilities like _.camelCase, _.capitalize, _.kebabCase, and _.truncate for common string manipulations

Resources

MDN - String

docs

JavaScript.info - Strings

article

Related Questions

What is the difference between map(), filter(), and reduce() array methods?

junior
arrays
Previous
How do destructuring assignment and spread/rest operators work in JavaScript?
Next
How do you select and manipulate DOM elements in JavaScript?
PrevNext