Get first two characters of string JavaScript HTML example code You can use inbuilt slice method to get first 2 characters of a string in JavaScript. Pass index value 0 for start and 1 for end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE HTML> <html> <body> <script> var str = "Hello world, Simple example"; var strFirst2 = str.substring(0,2); alert(strFirst2); </script> </body> </html> |
Output: Do comment if you have any doubts and suggestions on this JS char topic. RohitDegree in Computer Read More…