
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the 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>
- Related Articles
- What is the use of substr() method in JavaScript?
- What is the difference between == and === in JavaScript?
- What is the difference between comments /*...*/ and /**...*/ in JavaScript?
- What is the difference between Java and JavaScript?
- What is the difference between JavaScript and ECMAScript?
- What is the difference between JavaScript and C++?
- What is the difference between jQuery and JavaScript?
- What is the difference between null and undefined in JavaScript?
- What is the difference between Bower and npm in JavaScript?
- What is the difference between call and apply in JavaScript?
- What is the difference between window.onload and document.onload in Javascript?
- What is the difference between functions and methods in JavaScript?
- What is the difference between setTimeout() and setInterval() in JavaScript?
- What is the difference between getter and setter in JavaScript?
- What is the difference between a++ and ++a in JavaScript?

Advertisements