JavaScript Example: Get the length of the string

Updated on     Kisan Patel

If we want to get the length of the string in javascript, we need to use javascript string length proprty.

The javascript string length property returns the number of characters in a string.

<!-- HTML -->
Name: <input type="text" id="txtName" name="txtName" />
<input type="button" value="Get Text Length" onclick="GetLength()" />

<!-- Javascript -->
<script type="text/javascript">
function GetLength() {
 var text = document.getElementById("txtName").value;
 alert("The length of the Name you entered: " + text.length);

In the above code snippet, we are getting the value of the textbox on click of the button in text variable. Then calling the length property of javascript string object to get the length of the string in the alert box.

See the Pen aOxvRa by Kisan (@pka246) on CodePen.


Leave a Reply