The String substring() method
Find out all about the JavaScript substring() method of a string
substring()
returns a portion of a string and it’s similar to slice()
, with some key differences.
If any parameter is negative, it is converted to 0
.
If any parameter is higher than the string length, it is converted to the length of the string.
So:
'This is my car'.substring(5) //'is my car'
'This is my car'.substring(5, 10) //'is my'
'This is my car'.substring(5, 200) //'is my car'
'This is my car'.substring(-6) //'This is my car'
'This is my car'.substring(-6, 2) //'Th'
'This is my car'.substring(-6, 200) //'This is my car'
→ Here's my latest YouTube video
→ Get my JavaScript Beginner's Handbook
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025