the logic behind itI have the following code that I made based on an example from MDN.
function spinalCase(str){
return str = str.replace(/[A-Z]/g,function(match, offset, string) {
return (offset > 0 ? '-' : '') + match.toLowerCase();
});
}
Would someone please explain me how the following line of code works?
return (offset > 0 ? '-' : '') + match.toLowerCase();
I've tried to understand it based on the replace() method page from MDN but so far I don't understand the logic behind it.
What I mostly don't understand is how (offset > 0 ? '-' : '') + match.toLowerCase() returns the word with the replacement. Does the replace function do a loop to iterate through all letters? Or does it just returns the entire ready string with the replacement...