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
  • 299
  • 1
  • 3
  • 9

2 Answers2

1

It converts it to a number.

+"234" => 234

c2huc2hu
  • 2,336
  • 15
  • 23
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,219
  • 1
  • 16
  • 27