Which of the following library functions could be used to determine the length of a string?

The length read-only property of a string contains the length of the string in UTF-16 code units.

Try it

Description

This property returns the number of code units in the string. JavaScript uses UTF-16 encoding, where each Unicode character may be encoded as one or two code units, so it's possible for the value returned by length to not match the actual number of Unicode characters in the string. For common scripts like Latin, Cyrillic, wellknown CJK characters, etc., this should not be an issue, but if you are working with certain scripts, such as emojis, mathematical symbols, or obscure Chinese characters, you may need to account for the difference between code units and characters.

The language specification requires strings to have a maximum length of 253 - 1 elements, which is the upper limit for precise integers. However, a string with this length needs 16384TB of storage, which cannot fit in any reasonable device's memory, so implementations tend to lower the threshold, which allows the string's length to be conveniently stored in a 32-bit integer.

  • In V8 (used by Chrome and Node), the maximum length is 229 - 24 (~1GB). On 32-bit systems, the maximum length is 228 - 16 (~512MB).
  • In Firefox, the maximum length is 230 - 2 (~2GB). Before Firefox 65, the maximum length was 228 - 1 (~512MB).
  • In Safari, the maximum length is 231 - 1 (~4GB).

For an empty string, length is 0.

The static property String.length is unrelated to the length of strings. It's the arity of the String function (loosely, the number of formal parameters it has), which is 1.

Since length counts code units instead of characters, if you want to get the number of characters, you can first split the string with its iterator, which iterates by characters:

function getCharacterLength(str) {
  // The string iterator that is used here iterates over characters,
  //  not mere code units
  return [...str].length;
}

console.log(getCharacterLength("A\uD87E\uDC04Z")); // 3

Examples

Basic usage

const x = "Mozilla";
const empty = "";

console.log(`${x} is ${x.length} code units long`);
/* "Mozilla is 7 code units long" */

console.log(`The empty string has a length of ${empty.length}`);
// expected output: "The empty string has a length of 0"

Strings with length not equal to the number of characters

const emoji = "😄";
console.log(emoji.length); // 2
const adlam = "𞤲𞥋𞤣𞤫";
console.log(adlam.length); // 8
const formula = "∀𝑥∈ℝ,𝑥²≥0";
console.log(formula.length); // 11

Assigning to length

Because string is a primitive, attempting to assign a value to a string's length property has no observable effect, and will throw in strict mode.

const myString = "bluebells";

myString.length = 4;
console.log(myString);
// expected output: "bluebells"
console.log(myString.length);
// expected output: 9

Specifications

Specification
ECMAScript Language Specification
# sec-properties-of-string-instances-length

Browser compatibility

BCD tables only load in the browser

See also

What is another term for input validation?

Input Validation, also known as data validation, is the testing of any input (or data) provided by a user or application against expected criteria.

Is another term for input validation quizlet?

Terms in this set (20) An input validation loop is sometimes called an error handler. Checking for accuracy of data, even when the user provides the right type of data, is part of input validation.

What is the first input value for the validation of a loop?

The first input operation—just before the loop — is called a priming read, and its purpose is to get the first input value that will be tested by the validation loop.

Which of the following error occurs when a real value is attempted to be assigned to an integer variable?

The Overflow error occurs when a variable is assigned a value it cannot contain. This typically relates to Integer variables, which can only hold values up to 32767. If a value such as 40000 is assigned to an Integer, an Overflow error will occur.