How to insert javascript variables into a URL

By using the right quotes:

var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=" + a + "&sublist=" + b;

If you start a string with doublequotes, it can be ended with doublequotes and can contain singlequotes, same goes for the other way around.


var a = 1;
var b = 2;
var mylink = `http://website.com/page.aspx?list=${a}&sublist=${b}`;

Copied from above answer.

Do notice that it is not single quote.

enter image description here


In modern JavaScript standards we are using ${var} :

var a = 1;
var b = 2;
var mylink = `http://website.com/page.aspx?list=${a}&sublist=${b}`;