Explain string substr() in js


Really Dumb



Explain string substr() in js
The string substr() method in JavaScript is a way to take a string and extract a certain number of characters from it. For example, if you had a string that said “Hello World” and you wanted to extract the word “Hello”, you could use the substr() method to do that. To use the substr() method, you must provide two arguments: the beginning index and the length. The beginning index is the number of the character in the string where you want to start your extraction. The length is the number of characters you want to extract. So, in the example above, the beginning index would be 0 and the length would be 5, since “Hello” has 5 characters. Here’s an example of the string substr() method in action: let greeting = “Hello World”; let extractedWord = greeting.substr(0, 5); console.log(extractedWord); // Output: “Hello” Fun Fact: The string substr() method is similar to the string slice() method. The main difference is that the beginning index can be a negative number with the slice() method. Hope this helps!