2

I'm trying to figure out how some javascript code for creating pagination is working and I ran across this line:

if (+a[i].innerHTML === Pagination.page) a[i].className = 'current';

I was just wondering what the + is doing in this +a[i]. Here is the rest of that section of code just in case.

Bind: function() {
    var a = Pagination.e.getElementsByTagName('a');
    for (var i = 0; i < a.length; i++) {
        if (+a[i].innerHTML === Pagination.page) a[i].className = 'current';
        a[i].addEventListener('click', Pagination.Click, false);
    }
},
user2052567
  • 409
  • 1
  • 5
  • 11

2 Answers2

1

It converts it to a number.

+"234" => 234

c2huc2hu
  • 2,447
  • 17
  • 26
0

It's the unary plus operator:

The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already

For example

+"15" === 15
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28