String.prototype.split()

I don't understand why the ECMA-262 algorithm doesn't process the literal number.
In the ECMA-262 2024 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...

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

1 Like

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 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.

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

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?

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. :thinking:

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.

1 Like

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?

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

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.

1 Like

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...