Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.39 KB

2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md

File metadata and controls

40 lines (32 loc) · 1.39 KB
layout title tip-number tip-username tip-username-profile tip-tldr redirect_from categories
post
Writing a single method for arrays and a single element
6
mattfxyz
Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (`css` will modify everything matched by the selector).
/en/writing-a-single-method-for-arrays-and-a-single-element/
en
javascript

Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (css will modify everything matched by the selector).

You just have to concat everything into an array first. Array.concat will accept an array or a single element.

function printUpperCase(words) {
  var elements = [].concat(words || []);
  for (var i = 0; i < elements.length; i++) {
    console.log(elements[i].toUpperCase());
  }
}

printUpperCase is now ready to accept a single node or an array of nodes as its parameter. It also avoids the potential TypeError that would be thrown if no parameter was passed.

printUpperCase("cactus");
// => CACTUS
printUpperCase(["cactus", "bear", "potato"]);
// => CACTUS
//  BEAR
//  POTATO