title: Locally scoped variable is not defined in function
body: <p>I have a function <code>currentPlayer</code> as follows:</p>
  
  <pre><code>function currentPlayer(players) {
    return players.findIndex(player =&gt; player.current) || 0;
  }
  </code></pre>
  
  <p>I am trying to use this function in a reducer as follows:</p>
  
  <pre><code>export function players(players = [], action) {
    players = players.slice();
    switch (action.type) {
      ...
      case DELETE_PLAYER:
        const currentPlayer = currentPlayer(players);
        if (currentPlayer === action.index) {
          players = switchNextPlayer(players, currentPlayer);
        }
        players.splice(action.index, 1);
  
    }
    return players;
  }
  </code></pre>
  
  <p>On running this code, I get the following error: </p>
  
  <blockquote>
    <p>ReferenceError: currentPlayer is not defined</p>
  </blockquote>
  
  <p>The code works if I change the variable name as follows:</p>
  
  <pre><code>const currentPlayerIndex = currentPlayer(players);
  </code></pre>
  
  <p>I thought that the variable <code>currentPlayer</code> would be scoped in the reducer function. What is going on here?</p>