GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript String substring() Method - GeeksforGeeks JavaScript String substring() MethodLast Updated : 16 Jan, 2026 The substring() method is used to extract a portion of a string. It returns a new string without changing the original one.
Syntax string.substring(startIndex, endIndex);Parameters
Return value
Extracting Substrings by Character IndexOne common use case for substring() is when you need to extract specific substrings from a known index range. For example, you might extract the first name or last name from a full name string.
Extracting a Portion of a URLYou can use substring() to extract parts of a URL, such as the protocol, domain, or path.
String Validationsubstring() can be used in string validation checks, such as checking whether a specific portion of a string matches a pattern.
Removing a Prefix or SuffixIf you need to remove a prefix or suffix from a string, you can use substring() to extract the part of the string that remains.
Handling Negative IndicesThe substring() method does not support negative indices; it converts them to 0, so extraction always starts from the beginning of the string rather than from the end.
When the Starting Index is GreaterIf the start index is greater than the end index, substring() automatically swaps them, ensuring the correct substring is extracted without errors.
Using Only the Starting IndexIf only the starting index is provided, substring() will return the substring from that index to the end of the string.
ImmutabilityLike most string methods in JavaScript, substring() does not alter the original string. Instead, it returns a new string.
Use substring() in JavaScript
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |