Description
The length property in JavaScript is used to find the total number of characters in a string. This includes:
- Letters (uppercase and lowercase)
- Numbers
- Special characters (e.g., @, #, !)
- Spaces
- Unicode characters
Key Features
- It is a read-only property (cannot be changed directly).
- It counts all characters, including spaces and special symbols.
- Works on both string literals and string variables.
Syntax
The syntax for using the length property is simple:
string.length;
Here, string is any valid JavaScript string variable or a string literal.
Example 1: Finding Length of a String
let str = "Coding Ninjas";
console.log(str.length);

You can also try this code with Online Javascript Compiler
Run Code
Output:
13
Example 2: Using length in a Function
function getStringLength(input) {
return input.length;
}
console.log(getStringLength("JavaScript"));
console.log(getStringLength("Learn coding"));

You can also try this code with Online Javascript Compiler
Run Code
Output:
10
12
Return Value
The length property returns a number representing the total characters in the string.
Example
let sample = "Hello World!";
console.log(typeof sample.length);

You can also try this code with Online Javascript Compiler
Run Code
Output:
number
Since length returns a number, it can be used in arithmetic operations as well.
Example: Using length for Validation
let password = "abc123";
if (password.length < 8) {
console.log("The Password is too short!");
} else {
console.log("The Password is strong.");
}

You can also try this code with Online Javascript Compiler
Run Code
Output:
The password is too short!
Generate Alphabetic String With Apache Commons Lang
Generating a random alphabetic string means creating a string that contains only letters (A-Z, a-z). This is useful when you need a string without numbers or special characters, like generating a random name or a placeholder text. The Apache Commons Lang library provides a simple way to achieve this.
Step 1: Add Apache Commons Lang to Your Project
Before you can use the library, you need to add it to your project. If you're using Maven, add the following dependency to your `pom.xml` file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
If you're not using Maven, you can download the JAR file from the [Apache Commons Lang website](https://commons.apache.org/proper/commons-lang/) & add it to your project manually.
Step 2: Use `RandomStringUtils` to Generate Alphabetic Strings
The `RandomStringUtils` class in Apache Commons Lang provides a method called `randomAlphabetic()` to generate random alphabetic strings. Let’s see how you can use it:
import org.apache.commons.lang3.RandomStringUtils;
public class RandomStringExample {
public static void main(String[] args) {
// Generate a random alphabetic string of length 10
String randomString = RandomStringUtils.randomAlphabetic(10);
System.out.println("Random Alphabetic String: " + randomString);
}
}
In this Code:
1. Import the Library: We import the `RandomStringUtils` class from the `org.apache.commons.lang3` package.
2. Generate the String: The `randomAlphabetic(int length)` method generates a random string of the specified length. In this example, the length is 10.
3. Print the Result: The generated string is printed to the console.
Example Output
When you run the above code, you might get an output like this:
Random Alphabetic String: qWzRtYpLmN
This string will contain only uppercase & lowercase letters. Each time you run the program, a new random string will be generated.
Customizing the String Length
You can change the length of the string by passing a different integer value to the `randomAlphabetic()` method. For example, to generate a string of length 15, you can modify the code like this:
String randomString = RandomStringUtils.randomAlphabetic(15);
Browser Support
The length property is supported by all modern browsers, including:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Apple Safari
- Opera
- Internet Explorer
It has been part of JavaScript since ECMAScript 1 (1997), so it works across all JavaScript environments.
Frequently Asked Questions
Can the length property be modified?
No, the length property is read-only. You cannot change it directly.
Does length count spaces and special characters?
Yes, the length property counts all characters, including spaces and special symbols.
Can we use length on an empty string?
Yes, the length of an empty string is 0.
Conclusion
In this article, we explored the length property of JavaScript strings. This property helps determine the number of characters in a string, including spaces and special characters. Since it is a read-only property, it provides an easy and efficient way to handle string operations in JavaScript. Understanding its usage is essential for effective string manipulation in web development.