Skip to content

JavaScript get the first 3 characters of a string | HTML example code

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:

JavaScript get the first 3 characters of a string

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

1 thought on “JavaScript get the first 3 characters of a string | HTML example code”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading