Skip to main content
2 of 2
updated to fix readability and note ECMAScript 6 destructuring support
svidgen
  • 15.3k
  • 3
  • 40
  • 63

It depends entirely on what the calling code is expected to do with the returned coordinates. If your current code just needs direct access to the x and y properties, use the literal:

return {x: x, y: y};

Or with modern and/or transpiled JavaScript:

return { x, y };

If you need a more sophisticated return value, just change it:

return new Coordinates(x, y);

Or this:

return BuildCoordinate(x, y);

As long as the new return-value has x and y properties (or getters), your legacy code will still work.

As noted in Sayem's answer, you can also "destructure" return values in ECMAScript 6, or if you're using a web-aware transpiler/builder:

let { x, y } = get_coords();
svidgen
  • 15.3k
  • 3
  • 40
  • 63