JavaScript – String Length
To get length of a string in JavaScript, read the length property of the string instance.
Syntax
The syntax to read length property of a string instance str
is
</>
Copy
str.length
Examples
The following is a quick example to get the length of a string in JavaScript.
</>
Copy
var str = 'hello world';
var len = str.length;
In the following example, we take a string str
in script, and find its length. We shall display the string length in a div.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var str = 'hello world';
var len = str.length;
document.getElementById('output').innerHTML = 'String length : ' + len;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to get the length of a string in JavaScript, using length property, with examples.