5

what is the difference between .split(), .split('') and .split(' ') ?

Comments
  • 11
    hate to be that guy but...google it! Or try it yourself, you have a JS console in every browser. You should never be afraid to just go for it with stuff like this!

    My tests got me this:

    "Hello World!".split()
    >["Hello World!"]

    "Hello World!".split('')
    >(12) ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"]

    "Hello World!".split(' ')
    >(2) ["Hello", "World!"]

    So, looks like no args gives you back your input, blank delimiter gives you back an array of chars, and then a space character is making your delimiter a space, so it will split the word on each space. I'm not super familiar with JS but I'm guessing most of this is right
  • 1
    @tpanfl FTFY: No args gave you your original input _as the single item in an array_, which conforms with the idea of a split, but tells us that no split was actually performed for that test case.

    I'm not a JS developer, furthermore I'm on mobile without access to an interpreter ATM, but maybe split(noargs) does newlines?
  • 0
    @tokumei I think that would be split("\n")
  • 2
    dR != SO
Add Comment