Home  >  Article  >  Web Front-end  >  What is the method to find the length of a string in JavaScript?

What is the method to find the length of a string in JavaScript?

青灯夜游
青灯夜游Original
2021-06-23 16:19:547133browse

In JavaScript, you can use the length property of the String object to find the length of the string. This property can return the length of the string (number of characters). The length is in characters; the syntax format is "string.length".

What is the method to find the length of a string in JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript strings are used to store and process text. String indexing starts at 0, which means the first character index is [0], the second is [1], and so on.

JavaScript to find the length of a string

In JavaScript, the length of a string can be read using the length property of the string. The length is in characters and this property is read-only.

Example:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">  
</head>
<body>

<script>
var txt = "Hello World!";
document.write(txt.length);
</script>

</body>
</html>

Output:

12

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the method to find the length of a string in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn