# String.prototype.split()

**URL:** <https://es.discourse.group/t/string-prototype-split/1823>\
**Category:** I have questions\
**Created:** [October 16, 2023, 8:10pm UTC](https://es.discourse.group/t/string-prototype-split/1823 "2023-10-16T20:10:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxPScript](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxpscript/32/1866_2.png) [@MaxPScript](https://es.discourse.group/u/MaxPScript)\
**Post date:** [October 16, 2023, 8:10pm UTC](https://es.discourse.group/t/string-prototype-split/1823/1 "2023-10-16T20:10:02Z")

</div>

I don't understand why the ECMA-262 algorithm doesn't process the literal number.  
In the ECMA-262 2024 [String.prototype.split()](https://tc39.es/ecma262/multipage/text-processing.html#sec-string.prototype.split) in a 22.1.3.23 section step N1 return a Number object from a number literal (if number literal was passed in as an argument, for example, 12345). In a step N3 this Number object process to a literal string. Next, the algorithm processes a regular string "12345". Accordingly, this code causes an error ('num1.split is not a function'). And you need to explicitly convert the number to a string . Please explain what I misunderstood. Thank you!  
I'm sorry, I don't know how to paste the JS code here...

---

<div class="post-metadata">

**Author:** ![jmdyck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jmdyck/32/796_2.png) [@jmdyck](https://es.discourse.group/u/jmdyck)\
**Post date:** [October 16, 2023, 10:09pm UTC](https://es.discourse.group/t/string-prototype-split/1823/2 "2023-10-16T22:09:43Z")

</div>

If you put a line of 3 backticks before and after your code, discourse will preserve its whitespace.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [October 17, 2023, 12:47am UTC](https://es.discourse.group/t/string-prototype-split/1823/3 "2023-10-17T00:47:38Z")

</div>

While many of the different methods in JavaScript's built-ins are very versatile and can work on many different kinds of values (Array methods are notably [generic](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods) in this fashion) that capability does not determine which objects have direct access to those methods.

Since `split()` is a method that targets strings, it is only accessible off of string values, it living in `String.prototype`. Numbers look to `Number.prototype` for their methods and it only contains methods related to numbers, not those specific to strings, or any other data type for that matter (excluding Object).

So yes, `split()` can work with numbers, or any non-nullish value that can be coerced into a string, but that doesn't mean it will be accessible directly from any of those values. If you want to use it with a non-string value, you can, either by converting the value to a string first, or by grabbing the `split` function from `String.prototype` (or any string) and calling it against your desired value.

```typescript
const { split } = String.prototype;
const num1 = 12345;
const digits = split.call(num1, "");
console.log(digits); // ["1", "2", "3", "4", "5"]

```

---

<div class="post-metadata">

**Author:** ![MaxPScript](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxpscript/32/1866_2.png) [@MaxPScript](https://es.discourse.group/u/MaxPScript)\
**Post date:** [October 17, 2023, 9:10am UTC](https://es.discourse.group/t/string-prototype-split/1823/4 "2023-10-17T09:10:12Z")

</div>

Thanks, guys, a lot for the clarifications! While I'm processing and building mental models, one moment, please. Do I understand the following correctly? Technically, the algorithm does not prohibit directly processing, for example, literal numbers along with literal strings. And is it the engines under the hood that impose such restrictions?

---

<div class="post-metadata">

**Author:** ![MaxPScript](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxpscript/32/1866_2.png) [@MaxPScript](https://es.discourse.group/u/MaxPScript)\
**Post date:** [October 17, 2023, 12:03pm UTC](https://es.discourse.group/t/string-prototype-split/1823/5 "2023-10-17T12:03:49Z")

</div>

Moreover, at the beginning of the algorithm there is an introductory sentence. This method returns an Array into which substrings of the result of converting this object to a String have been stored. 🤔

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 17, 2023, 12:45pm UTC](https://es.discourse.group/t/string-prototype-split/1823/6 "2023-10-17T12:45:08Z")

</div>

It’s not a “restriction”, it’s just that the method is only available on String.prototype. The algorithm coerces its receiver to a string, so it would work with bigints, objects, arrays, etc - its just that the spec only defines it on String.prototype.

---

<div class="post-metadata">

**Author:** ![MaxPScript](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxpscript/32/1866_2.png) [@MaxPScript](https://es.discourse.group/u/MaxPScript)\
**Post date:** [October 17, 2023, 2:33pm UTC](https://es.discourse.group/t/string-prototype-split/1823/7 "2023-10-17T14:33:06Z")

</div>

Okay, I'll ask the question differently. If the algorithm has no explicit restrictions on processing a numeric value, then why does the browser throw an error?

```javascript
let s1 = 12345
let res = s1.split()
console.log(res) // s1.split is not a function

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 17, 2023, 2:44pm UTC](https://es.discourse.group/t/string-prototype-split/1823/8 "2023-10-17T14:44:23Z")

</div>

Because numbers don’t have a split method, only strings do.

A method has to be explicitly specified to be on a prototype for it to be accessible - and numbers and strings have different prototypes. It’s not that anyone excluded numbers, it’s just that they only added it for strings.

---

<div class="post-metadata">

**Author:** ![MaxPScript](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxpscript/32/1866_2.png) [@MaxPScript](https://es.discourse.group/u/MaxPScript)\
**Post date:** [October 17, 2023, 3:19pm UTC](https://es.discourse.group/t/string-prototype-split/1823/9 "2023-10-17T15:19:22Z")

</div>

Understood. Thank you! Perhaps I go too deep into the details in an attempt to understand what I am looking for and go to the very bottom, and the answer, it turns out, lies somewhere in the middle...
