Skip to content

JavaScript string length Property | length of a string (number of characters)

  • by

JavaScript String length Property is used the get the length of a given string. In the simple word, you can get the number of characters in the string (sentence). It returns the number of characters in a string.

Note: The length of an empty string is 0.

Syntax

No parameter required.

string.length

Return Value

It returns the number of characters in the given string.

Example of JavaScript string length Property

JS code to show the working of string.length property:

<!DOCTYPE html>
<html>
    <head>
        <title> Example</title>
        <script type="text/javascript">
		var str = "Hello World!";
		var n = str.length;
		// show output in alert
        	alert(n)
      
        </script>
    </head>
    
</html>

Output:

JavaScript string length Property

How can I find the length of a number?

Example of JavaScript length of number:-

var x = 1234567;

x.toString().length;

This process will also work forFloat Number and for Exponential number also.

Q: What is the JavaScript string length limit?

Answer: Is there a limit on length of the key (string) in JS object?

How browsers handle it, is another matter. Like Chrome 40 (Desktop), Chrome 40 (Android 5.1), Firefox 36, Opera 27, and IE9+ can deal with a property name of up to 227 characters. Safari 8 (OS X Yosemite) can even handle property names of 230 characters.

Source: https://stackoverflow.com/questions/13367391/

Q: How to javascript character count?

Answer: You can count the number of char in the string by using string length Property.

string.length

Note: The length property is a string property and not a string method.

Do comment if you have any questions and doubts in the tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading