August 30, 2025
JavaScript, get sub string
If you want to get sub string from a string so you can easily do this through JavaScript's built in function slice. This function required two parameters first parameter indicate index value from where you want to retrieve the sub string and second parameter indicate how many character you want to retrieve, this second parameter is optional if you do not pass third parameter then function consider you required all the characters from the given index (first parameter). The example of slice is mentioned below:
Code
<script>
msgVariable = "Hello, welcome in javascript world";
subMsg = msgVariable.slice(7);
alert(subMsg); // this will print message welcome in javascript world
subString = msgVariable.slice(7, 14); // 7 to 14 (exclusive)
alert(subString); //this will print message "welcome"
</script>