0
const buildUrl = (version, path) => `api/${version}/${path}`;

what does this do? How does this work? what is that =>

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • 5
    Possible duplicate of [What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?](https://stackoverflow.com/questions/24900875/whats-the-meaning-of-an-arrow-formed-from-equals-greater-than-in-javas) – VLAZ Sep 11 '18 at 20:23
  • 1
    Possible duplicate of [What is the usage of the backtick symbol (\`) in JavaScript?](https://stackoverflow.com/questions/27678052/what-is-the-usage-of-the-backtick-symbol-in-javascript) – Luca Kiebel Sep 11 '18 at 20:24

2 Answers2

1

The => is whats called an arrow function, read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

These two are the same with a few small differences, that doesn't come into effect here:

(version, path) => `api/${version}/${path}`

function(version, path) {
  return `api/${version}/${path}`
}

if the backticks (``) in the string are used to create template string, so they can a string and place variables (version, path) into them as denoted by the ${}.

`api/${version}/${path}`

If the version is 1.0.0 and path is home, then the string template will return api/1.0.0/home

Read more about template strings here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

dotconnor
  • 1,736
  • 11
  • 22
0

The lamda => causes your buildUrl constant to turn into a function that can be called like any other function where "version" and "path" become parameters of your function.

buildUrl("test1","test2")

… would simply return "api/test1/test2"

Surrounding your string with the back ticks "`" will cause the string to be interpolated which causes each variable contained in ${somevar} to be evaluated or rendered.

peter.edwards
  • 335
  • 1
  • 3