length property in JavaScript String : Strings in JavaScript are sequence of characters. What if you want to know the number of characters a string has, solution is call the property called length on string object. JavaScript string has the property called length to know the length of the string.
If you call length property on JavaScript string it will return the number of characters that string has and if the string is empty it will return the 0. The property length is supported by all major browsers.
Syntax to use length property :
string.length
Example of length property in JavaScript : Here is the simple example which is returning the length of the code.
<!DOCTYPE html>
<html>
<head><title>My Page </title></head>
<body>
<p>Click to see the number of characters in the string "I live in India.".</p>
<button onclick="showLength()">Show Length</button>
<p id="container"></p>
<script>
function showLength() {
var msg = "I live in India.";
var len = msg.length;
document.getElementById("container").innerHTML = len;
}
</script>
</body>
</html>
Output : 16
So when we click in the Show Length button it will show us the length of the msg object which is 16.
0 Comment(s)