Sam Niles
Sam Niles

Reputation: 31

JavaScript replace() method best practice

I'm somewhat new to programming, so looking for some best practice advice. Is there a better way to write the following code, rather than using the replace() method twice?

const URL = 'user/profile/:id/result?size=:query';
const URI = URL.replace(':id', '1111').replace(':query', 100); //'user/profile/1111/result?size=100'

Upvotes: 0

Views: 130

Answers (1)

mike510a
mike510a

Reputation: 2168

you can use templates by wrapping the string with back ticks (```) and using ${somevalue}

var size = 100
var id = 1111
const URL = `user/profile/${id}/result?size=${size}`

Upvotes: 1

Related Questions