0

Can someone explain this comment which I have received for me? Thank you.

 const path = this.basePath + '/configurations/${id}/LicensePackageInfo'.replace('${' + 'id' + '}', String(id));

const path = this.basePath + /configurations/${id}/LicensePackageInfo ; should be fine. Dont use replace Func, the above code is using ` character to replace "replace " function

Nana
  • 23
  • 4
  • Does this answer your question? [What does ${} (dollar sign and curly braces) mean in a string in JavaScript?](https://stackoverflow.com/questions/35835362/what-does-dollar-sign-and-curly-braces-mean-in-a-string-in-javascript) – gunr2171 Aug 30 '22 at 19:27
  • Just use ` instead of ', you don't need to replace anything.. – Shai Aug 30 '22 at 19:28

1 Answers1

0

the replace function takes a substring and replaces each match in a string with it. You do not need to replace anything, the URL is correctly formatted as it is.

const path = this.basePath + `/configurations/${id}/LicensePackageInfo`

works due to a concept called string interpolation.

In JavaScript, you can put variables values inside strings created with backticks ` wrapping them in the syntax ${} you tried to replace.

const basePath = 'https://google.com'
const id = 10
const path = basePath + `/configurations/${id}/LicensePackageInfo`

console.log(path) // https://google.com/configurations/10/LicensePackageInfo

String interpolation example

const id = 10;
console.log(`my id is: ${id}`) //my id is: 10
console.log("my id is: ${id}") //my id is: ${id}

To read more on the concept https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Jonas Grønbek
  • 1,709
  • 2
  • 22
  • 49