The String object's length property

Last updated: February 23, 2025

The String object's length property returns the number of characters in a string.

Syntax

string.length

Parameters

Not applicable. The length property does not accept any parameters since it is a property, not a method.

Return value

The length property returns a number representing the number of characters in the string.

Examples

Example 1: The length property returns the total number of characters in a string

In the code below, the function lengthEx1() logs the number of characters in different strings.

function lengthEx1() {
  let str1 = "Hello world!";
  let str2 = ""; // Empty string
  let str3 = " "; // String with one space
  
  // counts all characters including spaces
  Logger.log(str1.length);
  
  // empty string has zero length
  Logger.log(str2.length);
  
  // space is counted as a character
  Logger.log(str3.length);
}

Output

12.0

0.0

1.0

Example 2: The length property counts special characters and Unicode characters

In the code below, the function lengthEx2() demonstrates how the length property counts special characters and Unicode characters.

function lengthEx2() {
  let str1 = "Hello\nWorld"; // String with newline
  let str2 = "๐ŸŒWorld"; // String with emoji
  
  // newline counts as one character
  Logger.log(str1.length);
  
  // emoji counts as 2 characters
  Logger.log(str2.length);
}

Output

11.0

7.0

Example 3: Using length property for string validation

In the code below, the function validateString() uses the length property to check if a string meets certain length requirements.

function validateString() {
  let username = "user123";
  let password = "pass";
  
  // Check if username is between 6 and 12 characters
  if (username.length >= 6 && username.length <= 12) {
    Logger.log("Username length is valid");
  } else {
    Logger.log("Username must be between 6 and 12 characters");
  }
  
  // Check if password is at least 8 characters
  if (password.length >= 8) {
    Logger.log("Password length is valid");
  } else {
    Logger.log("Password must be at least 8 characters");
  }
}

Output

Username length is valid Password must be at least 8 characters

Conclusion

In this tutorial, you learned how to use the length property of the String object to determine the number of characters in a string. This property is particularly useful for string validation and text processing tasks in Apps Script.

DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.

Small Scripts, Big Impact

Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation

Exclusive Google Sheets automation tutorials and hands-on exercises
Ready-to-use scripts and templates that transform hours of manual work into seconds
Email updates with new automation tips and time-saving workflows

By subscribing, you agree to our Privacy Policy and Terms of Service