How to usestring.substr() method in Javascript
Syntax:
let temp = str.substr(0, 3);
Example: In this example, we are using string.substr() method.
let originalString = "w3wiki";
let firstThreeChars = originalString.substr(0, 3);
console.log(firstThreeChars);
Output
Gee
How to get the first three characters of a string using JavaScript ?
In this article, we are going to learn how to get the first three characters of a string using JavaScript.
Example:
Input: today
Output: tod
Input: tomorrow
Output: tom
These are the approaches by which we can get the first three characters of a string using JavaScript:
Table of Content
- Approach 1: Using Slice() method
- Approach 2: Using string.substr() method
- Approach 3: Using For loop
- Approach 4: Using string.substring() method
- Approach 5: Using Array.from(), slice(), and join() methods