Skip to content

Discussion Topic: Var vs Const vs Let - Within Functions #6

@alishalabi

Description

@alishalabi

(https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript)

Context:
Throughout the course of this tutorial, I found converting all of the var variables with const or let variables (based on Airbnb's styleguide) was a great mental exercise . While many of these conversions were initially obvious (ex: Lives should almost always be let), many were not (ex: Should brickRowCount be let or const?).
Inevitably, for the less obvious variables - I found the best way to make a determination is to mentally go through your features and mentally identify which will change - shift those variable from (the default) const' to let`.

Discussion Point 1:
While the above method is thorough, it can take extra time. Are there any shortcuts used in industry to make a determination?

Discussion Point 2 :
How does this concept change when we are going through for loops?

Ex:

// Collision Detection (Brick)
    function collisionDetection() {
      for(var c=0; c<brickColumnCount; c++) {
        for(var r=0; r<brickRowCount; r++) {
          let b = bricks[c][r]
          if(b.status == 1) {
            if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
              dy = -dy;
              b.status = 0;
              score++;
              if(score == brickRowCount*brickColumnCount) {
                alert("YOU WIN, YOU ROCK!")
                document.location.reload()
                clearInterval(interval)
              }
            }
          }
        }
      }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions