0

I saw another stackoverflow thread here

one of the answers given was

function reverse(s){
    return s.split("").reverse().join("");
}

so my question is, why are there quotation marks for split and join but not reverse?

Community
  • 1
  • 1
slynthin
  • 65
  • 3
  • 2
    Learn JavaScript First. – Bhojendra Rauniyar Aug 14 '14 at 05:01
  • The close vote is not necessary, the question has a very finite answer is not to broad. – Liviu Mandras Aug 14 '14 at 05:06
  • 1
    Also I don't know why so many down votes for this question, granted it could have been answered by the author if he did some research but, AFAIK this is still a valid question on SO, and anyone is encouraged to ask programming questions here which do now fall under one of the 'close' reasons. – Liviu Mandras Aug 14 '14 at 05:08
  • @LiviuM. The help text for a downvote _literally begins_ __this question does not show any research effort__. – Evan Davis Aug 18 '14 at 19:14

3 Answers3

5

That's because the 3 functions are different:

String.split( delimiter ); // delimiter = split by what

Array.reverse(); // reverse an array
                 // unlike other array functions such as sort() or join()
                 // there is no other option to specify because the function
                 // does only one thing only one way

Array.join( join_string ); // join_string = what string to insert
                           // between elements

Whenever you have doubts about how functions work read the documentation:

Not all functions require one argument. Some take none. Some take two or three. Some have optional arguments.

slebetman
  • 109,858
  • 19
  • 140
  • 171
4

reverse() is called on an array. The result of split("") being an array of letters (strings of one letter each to be precise).

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
1

.split("") Will spit a string based on a given parameter. in this case "" means no delimiter so it will split on each char and return an array of char. then reverse() method is call on that array. once it reversed then we have to join it back to the string. join("") will do that task for you. in this case "" means no delimiter for join so array will join each char together.

bugmagnet
  • 7,631
  • 8
  • 69
  • 131
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86