variable is undefined in else statement : <p>I am confused as to why I am getting an 'undefined' error in my code. I am trying to delete an id from an array using the variable of 'id', but it errors with 'undefined'. I have tried various options of moving the 'var id;' but still I get the error. I have marked in the code where the error is occuring and would be grateful if someone could help me to correct this error. Many thanks</p>

<pre><code>$(function() {
  info = [];
  $(document).on('click', '.rowChk', function() {
    var id;
    if (this.checked) {
      $('#rowClk').show();
      var currentRows = $(this).closest("tr");
      var rackid = currentRows.find("td:eq(0)").text();
      //    var rackidnumber = currentRows.find("td:eq(1)").html();
      var rackservice = currentRows.find("td:eq(2)").html();
      var rackactivity = currentRows.find("td:eq(3)").html();
      var rackdept = currentRows.find("td:eq(4)").html();
      var rackcompany = currentRows.find("td:eq(5)").html();
      var rackaddress = currentRows.find("td:eq(6)").html();
      var rackuser = currentRows.find("td:eq(7)").html();
      var rackitem = currentRows.find("td:eq(8)").html();
      var rackddate = currentRows.find("td:eq(9)").html();
      var rackdate = currentRows.find("td:eq(10)").html();

      id = rackid;

      data = {};
      data.rackids = id;
      //    data.idnumber = rackidnumber;
      data.service = rackservice;
      data.activity = rackactivity;
      data.dept = rackdept;
      data.company = rackcompany;
      data.address = rackaddress;
      data.user = rackuser;
      data.item = rackitem;
      data.intakedate = rackdate;
      data.destroydate = rackddate;
      info.push(data);
    } else {
      console.log(id); &lt;--- Showing as undefined
      var index = info.findIndex(function(item) {
        return item.id === id;
      });
      if (index !== -1) {
        info.splice(index, 1);
        if (info.length === 0) {
          $('#rowClk').css('display', 'none');
        }
      }
    }
  });
});
</code></pre>
