Javascript variables to innerHTML – Can't figure out where this "undefined" is coming from

I’m passing a "log" back and forth between game players via AJAX calls.

The first player takes their turn and it’s logged, the log is saved into the local object, then when they save the loc.log is written to the game.log. The next player gets the game object and adds game.log to their log. For some reason when the 2nd player loads it though an "undefined" is added to the start of it. Then when the 1st player reloads it there are 2 in a row, undefinedundefined. I can’t figure out why. Everything seems to be defined.

Here’s my code:

let loc = {log: ""}
let game = {log: ""};

function gameLog(message) {

    let dateTime = new Date().toLocaleString();
    let logMessage = '<i>' + dateTime + '</i><br>' + message + '<br><br>';
    $('log').insertAdjacentHTML('afterbegin', logMessage);
    loc.log = logMessage + loc.log;
}

function save() {
    game.log = loc.log;
}

function load(){
    $('log').innerHTML = game.log + $('log').innerHTML;
}

148 thoughts on “Javascript variables to innerHTML – Can't figure out where this "undefined" is coming from”

  1. Pingback: purchase viagra
  2. Pingback: canadian viagra
  3. Pingback: buy viagra online
  4. Pingback: viagra pill
  5. Pingback: cialis for men
  6. Pingback: order cialis
  7. Pingback: viagra cheap
  8. Pingback: viagra buy
  9. Pingback: cialis rezeptfrei
  10. Pingback: cialis dosis
  11. Pingback: viagra generico
  12. Pingback: sildenafil 50 mg
  13. Pingback: viagra samples