How to get the part of a string in JavaScript

How to get the part of a string in JavaScript

In this post we will give you information about How to get the part of a string in JavaScript. Hear we will give you detail about How to get the part of a string in JavaScriptAnd how to use it also give you demo for it if it is necessary.

In JavaScript, the Substring() method helps us to get the particular part of a string by using thestart index and end index.

const name = "jshype"                      //0 is start index 2 is end indexconsole.log(name.substring(0,2));// output --> "js"console.log(name.substring(1,4));// output --> "shy"

start index: In the first argument, we need to specify the start index.

end index: In the second argument, we need to specify the end index (end index is not included in the output).

If we specify only single argument, the substring() method returns the part of a string from that index up to end of the string.

const name = "jshype"console.log(name.substring(2))// output -->  "hype"

Similary, we can also use the slice() method to get the part of a string.

Here is an example:

const name = "jshype"console.log(name.slice(0,2));//output --> "js"const lang = "JavaScript"console.log(lang.slice(4,lang.length))//output --> "Script"

Note: String.prototype.substr() is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead.

Hope this code and post will helped you for implement How to get the part of a string in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US