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
?
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
?
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.
reverse()
is called on an array. The result of split("")
being an array of letters (strings of one letter each to be precise).
.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.