
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between substr and substring in JavaScript
The examples given above have the same output, but yes the method differs, with that the parameters too.
The substring() method the second parameter is the index to stop the search, whereas the second parameter of substr() is the maximum length to return.
Example
Let’s see an example for substr() method in JavaScript −
<html> <head> <title>JavaScript String substr() Method</title> </head> <body> <script> var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substr(1,2)); document.write("<br />(-2,2): " + str.substr(-2,2)); document.write("<br />(1): " + str.substr(1)); document.write("<br />(-20, 2): " + str.substr(-20,2)); document.write("<br />(20, 2): " + str.substr(20,2)); </script> </body> </html>
Example
Let’s see an example for substring() method in JavaScript −
<html> <head> <title>JavaScript String substring() Method</title> </head> <body> <script> var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substring(1,2)); document.write("<br />(0,10): " + str.substring(0, 10)); document.write("<br />(5): " + str.substring(5)); </script> </body> </html>
Advertisements