1

Is there an upper limit to the possible character length of strings in JavaScript, and ES6+ in particular?

Could you do this?

const wowThisIsALongString = `${collectedWorksOfWilliamShakespeare}`

[I'd write the collected works out by hand but am feeling lazy.]

If I understand correctly (and odds are that I don't), a JavaScript string is just a special kind of JavaScript Object, so there's technically no limit?

But maybe things are different in practice?

EDIT / UPDATE: As people have noted, a string primitive isn't an Object. I'd never thought of it as such until I checked the ECMAScript 2015 specs.

4.3.17 String value

primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer

NOTE A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.

4.3.18 String type

set of all possible String values

4.3.19 String object

member of the Object type that is an instance of the standard built-in String constructor

NOTE A String object is created by using the String constructor in a new expression, supplying a String value as an argument. The resulting object has an internal slot whose value is the String value. A String object can be coerced to a String value by calling the String constructor as a function (21.1.1.1).

So, when they write that, is the meaning that String objects are objects which contain strings, or ... something else?

Another Update: I think that Ryan has answered this below.

Mallory-Erik
  • 1,750
  • 1
  • 19
  • 24
  • 3
    A string is a primitive type in JavaScript, it is not an object. `String` is used under the hood but string literals are not objects. – Badacadabra May 13 '17 at 17:17
  • It depends on the implementation. [See](http://stackoverflow.com/questions/4695187/javascript-maximum-size-for-types) – drGrove May 13 '17 at 17:19
  • Why was this reopened. The duplicate was valid. –  May 13 '17 at 17:25
  • `so there's technically no limit?` No. Technically, there's always a limit. In actuality, with modern hardware and resources, that limit is unlikely to be reached. – vol7ron May 13 '17 at 17:25
  • @squint: The duplicate was about object keys and this question is about strings in general. – Ry- May 13 '17 at 17:26
  • @Ryan: Yeah, you're right. –  May 13 '17 at 17:28
  • 1
    Instead of reopening, add other duplicate questions, like this one: http://stackoverflow.com/questions/34957890/javascript-string-size-limit-256-mb-for-me-is-it-the-same-for-all-browsers. It is obvious this question has been asked several times before in different flavours. – trincot May 13 '17 at 17:39
  • 1
    About *String* vs. *string* : string is just a char[], however to make it possible to call methods on strings, they are *wrapped* by the *String* object: "hi".toString() ... – Jonas Wilms May 13 '17 at 17:45

1 Answers1

2

There is a specified length of 253 − 1 in Section 6.1.4:

The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (“elements”) up to a maximum length of 253-1 elements.

This is the highest integer with unambiguous representation as a JavaScript number:

> 2**53 === 2**53 - 1
false

> 2**53 === 2**53 + 1
true

Individual engines can have smaller limits. V8, for example, limits its strings to 228 − 14 characters.

Side note: primitive strings aren’t objects, but that doesn’t have much to do with length limits. JavaScript has a “primitive wrapper” misfeature allowing strings, numbers, and booleans to be wrapped by objects, and that’s what the section you linked refers to, but there’s no reason to ever use it.

Ry-
  • 218,210
  • 55
  • 464
  • 476