What is JavaScript Length

A data type or class that encompasses a collection of characters is referred to as a string. In JavaScript, we can process and manipulate strings through an array of properties and methods that the language provides. This enables us to add, modify, and delete content within strings. Additionally, we can perform various operations such as removing characters, finding specific characters, and altering their case. The String property in JavaScript includes a length property, which is one of the most essential and prominent attributes. Since strings are immutable, any method executed on a string will always yield a new string object; the original string remains unchanged and intact. The length of a string is calculated based on the total number of characters it comprises, akin to another object. Utilizing a string object, this property reveals the count of characters present in the string and activates the length property.

The returned value is an unsigned integer. When a string is transformed into a character array, the resulting length exceeds the highest index by one, as array indexing begins at zero.

The initial maximum length of a string was not defined. However, ECMAScript 2016 establishes this maximum length at 2^53 - 1. For Firefox, the maximum length is set at 2^30 - 2, which is nearly equivalent to 1 GB, thereby enhancing its capacity. Prior to version 65 of Firefox, the maximum string length was restricted to 2^28 - 1, which amounts to nearly 256MB. This represented the earlier maximum string capacity for Firefox.

Syntax

Example

var Stringlength = String.length;

In this instance, the expression on the right side produces a String object, while the variable Stringlength is utilized to store the count of characters contained within the String object.

String.length is equal to the number;

Here, the numeral indicates the maximum number of characters that the String object can hold.

JavaScript string length examples

In JavaScript, the property string.length is utilized to ascertain the length of a string. Below are several examples demonstrating the lengths of different strings in JavaScript:

Example 1: Determining a String's Length

Example

<!DOCTYPE html>
<html>
<body>
<p>Determining the string length property</p>
<button onclick="getLength()">Display the length of the string</button>
<p id="example"></p>
<script>
function getLength() {
var String = "Hello welcome to the tutorial"; var stringLength = String.length;
document.getElementById("example").innerHTML = stringLength;
}
</script>
</body>
</html>

Output

Before clicking the button:

After clicking the button:

The result shows that the phrase "Hello welcome to the tutorial" comprises a total of 29 characters. Upon clicking the button labeled "Display the length of the string," the getLength function is executed, which in turn presents this length on the display.

Example 2: Giving a String Object the Length

The functional effect of enlarging the existing string is minimal. Below is the syntax used to determine the length of a string:

Example

String.length = number;

Let's confirm it with the help of an example.

Example

<!DOCTYPE html>
<html>
<body>
<p>Determining the string length property</p>
<button onclick="assignLength()">Display the length of the string</button>
<p id="demo"></p>
<script>
let sampleString = "Javascript Strings";
document.getElementById("demo").innerHTML = "Length before assigning "+sampleString.length;
function assignLength() { sampleString.length = 5;
document.getElementById("demo").innerHTML = "Length after assigning "+sampleString.length;
}
</script>
</body>
</html>

Output

Before clicking the button:

After clicking the button:

Example 3: An empty string

An empty string contains no characters, which results in its length being initialized to zero by default. To confirm this, let’s examine an example:

Example

<!DOCTYPE html>
<html>
<body>
<p>Determining the string length property.</p>
<p id="demo"></p>
<script>
let sampleString = "";
document.getElementById("demo").innerHTML = "Length of the mentioned empty string is "+sampleString.length;
</script>
</body>
</html>

Output

Adaptability:

The subsequent web browsers are capable of utilizing the Length property for Strings, along with their minimum supported versions listed below.

Desktop Versions:

  • Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Opera
  • Safari

Mobile Versions:

  • Android webview
  • Chrome for android
  • Firefox for android
  • Opera for android
  • Safari on iOS
  • Samsung Internet
  • Node.js
  • In summary

We can conclude that the length property pertains to the maximum capacity of a String, specifically indicating the highest number of characters it can accommodate. Consequently, this property is employed in various internal string methods, such as replace, toLowerCase, splice, split, search, and others, when traversing and checking conditions. These methods internally convert the string into an array of characters and execute their respective logic on it. By utilizing the length property of a string, we can determine its size and length during conventional coding practices. This information is also advantageous when iterating through the string using the string iterator. Thus, the length property empowers us to manipulate Strings effectively within our code.

Input Required

This code uses input(). Please provide values below: