Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return join vs return string [closed]

Tags:

javascript

I saw this example and wondered why anyone would do it:

function a(str) {
  return [
    '<div>',
     str,
    '</div>'
  ].join('');
}

Isn't it an equivalent to the following code and what's the advantages / disadvantages of using just:

function a(str) {
  return '<div>' +
     str +
    '</div>;
}

Thank you.

like image 217
Fijir Avatar asked Sep 09 '26 09:09

Fijir


1 Answers

They're the same thing. Some experiments have shown using the + operator is faster but this will vary between browsers. Not to mention, these kinds of micro-optimizations don't tend to contribute much.

So which one is better? Whichever one you like the most.

like image 128
Mike Cluck Avatar answered Sep 11 '26 00:09

Mike Cluck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!