title: Variable reassigning itself - Javascript
body: <p>I'm running this chess AI, and it correctly assigns the <code>bestmove</code> variable, but when it does its final recursion and returns the score, and bestmove, the bestmove is always undefined even though it was previously set. Why is that?</p>
  
  <pre><code>var Buddha = function() {
  
    this.movehistory = 0;
    this.color = "b";
    this.opp = "w";
    this.bestmove = null;
  
    this.minimax = function(board, depth, alpha, beta) {
      if(depth &lt;= 0 || board.game_over() === true) {
        return [this.eval_board(board), this.bestmove]
      } else {
        if(board.turn() === this.color) {
  
          var possible_moves = board.moves();
          for (index = 0; index &lt; possible_moves.length; ++index) {
            if(possible_moves[index] != undefined) {
              var new_board = new Chess(board.fen());
              new_board.move(possible_moves[index]);
  
              var mini = this.minimax(new_board, --depth, alpha, beta);
              var score = mini[0],
                  move = mini[1];
  
             if(score &gt; alpha) {
                alpha = score;
                this.bestmove = possible_moves[index];
                console.log(this.bestmove);
                if(alpha &gt;= beta) {
                  break;
                }
              }
            }
          }
          return [alpha, this.bestmove]
  
        } else if(board.turn() === this.opp) {
  
          var possible_moves = board.moves();
          for (index = 0; index &lt; possible_moves.length; ++index) {
            if(possible_moves[index] != undefined) {
              var new_board = new Chess(board.fen());
              new_board.move(possible_moves[index]);
  
              var mini = this.minimax(new_board, --depth, alpha, beta);
              var score = mini[0],
                  move = mini[1];
  
              if(score &lt; beta) {
                beta = score;
                this.bestmove = possible_moves[index];
  
                if(alpha &gt;= beta) {
                  break;
                }
              }
            }
          }
          return [beta, this.bestmove]
        }
      }
    }
  
    this.eval_board = function(board) {
      if(board.in_check()) {
        if(board.turn() == this.opp) {
          return 999999999;
        } else {
          return -999999999;
        }
      } else if(board.in_checkmate()) {
        if(board.turn() == this.opp) {
          return 999999999;
        } else {
          return -999999999;
        }
      } else if(board.in_stalemate()) {
        if(board.turn() == this.opp) {
          return 999999999;
        } else {
          return -999999999;
        }
      } else {
        return 0;
      }
    }
  
    this.move = function(board) {
      console.log(this.minimax(board, 16, -999999999, 999999998));
    }
  
  }
  </code></pre>
  
  <p>My program uses the chess.js API found here:<a href="https://github.com/jhlywa/chess.js" rel="nofollow">https://github.com/jhlywa/chess.js</a></p>