The substring() method in JavaScript is a super handy way to extract specific parts of a string. Whether you’re cleaning up text or slicing out info, this method has your back.


🔍 What is substring()?

The substring() method extracts characters from a string, between two specified indices.

string.substring(start, end)
  • start: The index where extraction begins.
  • end (optional): The index before which to stop. If omitted, extraction goes to the end of the string.

✂️ Practical Examples

let str = "Hello, world!";

// Extract "world"
let substring1 = str.substring(7, 12); // "world"

// Extract from the beginning to "o"
let substring2 = str.substring(0, 4); // "Hell"

// Extract from "o" to the end
let substring3 = str.substring(4); // "o, world!"

đź’ˇ Key Points to Remember

  • substring() does not modify the original string—it returns a new one.
  • Indices are zero-based (first character is index 0).
  • If end is omitted, it extracts till the end of the string.
  • If start is greater than end, JavaScript automatically swaps them.

đź§  Quick Quiz

1. What is the purpose of the substring() method in JavaScript?

  • A. To concatenate two strings
  • B. To convert a string to uppercase
  • C. To extract a portion of a string based on specified indices
  • D. To replace a specific substring within a string

2. Which of the following is the correct syntax for using the substring() method?

  • A. string.substring(start, end)
  • B. substring(string, start, end)
  • C. string.extract(start, end)
  • D. substring(string, index)

🎯 Conclusion

The substring() method is a simple but powerful way to work with parts of strings in JavaScript. Once you get the hang of it, you’ll find it super useful in many string manipulation tasks.

Happy coding! 👨‍💻👩‍💻


<
Previous Post
Understanding PHP’s substr() Function: Demystifying Its Syntax
>
Next Post
đź’ˇ Code Clear: One Secret to Stress-Free Programming