javascriptsumgetparameter

Number Sum in Javascript


I can not do aggregation with JavaScript.

For example:

https://helloWorld.com/page?id=5

This is working: (Decrease)

var mevcutID = getParameterByName('id');
var newPageID = currentID - 1;
window.location.replace("https://helloWorld.com/page?id=" + newPageID);

## return: https://helloWorld.com/page?id=4

This is NOT working: (Addition)

var mevcutID = getParameterByName('id');
var newPageID = currentID + 1;
window.location.replace("https://helloWorld.com/page?id=" + newPageID);

## return: https://helloWorld.com/page?id=51

What is the reason of this?


Solution

  • You need to coerce currentID to an number. Right now it's a string and you're appending 1 as a string.

    var newPageID = +currentID + 1;