JavaScript | string.substring()
The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from start index to end index. Indexing start from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Parameters: Here the Startindex and Endindex describes the part of the string to be taken as substring. Here the Endindex is optional.
Return value: It returns a new string which is part of the given string.
Code #1:
<script> // Taking a string as variable var string = "geeksforgeeks"; a = string.substring(0, 4) b = string.substring(1, 6) c = string.substring(5) d = string.substring(0) // Printing new string which are // the part of the given string document.write(a + "<br>"); document.write(b + "<br>"); document.write(c + "<br>"); document.write(d + "<br>"); </script> |
Output:
geek eeksf forgeeks geeksforgeeks
Code #2:
Index alwways start with 0. If still we take index as negative, it will be considered as zero and index can’t be in fraction if it found so, it will be converted into its just lesser whole number.
<script> // Taking a string as variable var string = "geeksforgeeks"; a = string.substring(-1) b = string.substring(2.5) c = string.substring(2.9) // Printing new string which are // the part of the given string document.write(a + "<br>"); document.write(b + "<br>"); document.write(c + "<br>"); </script> |
Output:
geeksforgeeks eksforgeeks eksforgeeks
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Objects in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

