Use slice method to get first 3 characters of a string in JavaScript.
JavaScript get first 3 characters of string Example
A slice(begin, end) method works on strings, as well as arrays. It returns a string representing the substring of the original string, from beginning to end (end not included) where to begin and to end represent the index of characters in that string.
Let’s see HTML example code:
<html>
<body>
<script>
var str = "Hello world, Simple example";
var strFirstThree = str.substring(0,3);
console.log(strFirstThree);
</script>
</body>
</html>
Output:
Do comment if you have any doubts and suggestions on this JS char topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
No actual Output is given in the article…