From the MDN the split method separator is treated as a String or a RegExp. But 'asd'.split(['s']) correctly returns ['a', 'd'].
1 Answer
The specification for split includes the following step:
- Let R be ToString(separator).
So the array is coerced to a string, and the string representation of ['s'] is s.
It's worth noting that if you pass a regex to split, it doesn't get this far, as the regex object has an internal @@split method, which is used in step 3:
If separator is neither undefined nor null, then
a. Let splitter be GetMethod(separator, @@split).
b. ReturnIfAbrupt(splitter).
c. If splitter is not undefined , then
i. Return Call(splitter, separator, «O, limit»).
1 Comment
Mohit Bhardwaj
Yes, you're right, I just checked it with an example
console.log('as,df'.split(['s', 'd']) );
['s'].toString()gives's'.String(['s']) === 's'."111a,s222".split(['a','s'])splits since""+['a','s'] == 'a,s'