Don't reassign

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Don't reassign

To reassign a variable, you need to first declare a variable with let. Then, you assign another variable to it somewhere else in your code. This process is also called reassignment.

let name = 'Zell'
name = 'Vincy'

Why you should not reassign

There are two reasons:

  1. Reassignments create complicated code
  2. You might change external state accidentally

Reassignments create complicated code

When you create a variable with let, you tell your brain that the variable is going to change later. It needs to keep track of potential changes down the road (which can be taxing).

Imagine a colleague of yours changes her hairstyle everyday. Let’s say she comes in with bangs on Monday; braids on Tuesday; short hair on Wednesday; a waist-length, bright-pink wig on Thursday; and a shaved head on Friday…

Creating and tracking changes with let is similar (but less extreme) compared to tracking your colleague’s hair over the days. It’s tiring. It’s taxing.

This is the first reason why you should not reassign. For examples, check the “what to do instead” section. You’ll see why code that requires reassignments are more complicated than code that don’t use reassignments.

You might change external state accidentally

How do you reassign a value to a variable? You write variableName, followed by =, followed by your new value:

variableName = 'new value'

This seems okay at first glance, but it isn’t.

How do you know if your variable is scoped locally to the function you’re in, in this line of code, without checking anywhere else? How can you know for certain that you didn’t change the external state accidentally?

let name = 'Zell'

function changeName () {
  name = 'Vincy'
}

// External state changed. `name` is now 'Vincy'.
changeName()

If your code is somewhat complicated (and your code will be, trust me on this), you cannot guarantee you did not change the external state. This is especially true if your code is messy.

This is the second reason why you should not reassign variables.

What to do instead of reassignments

You should always declare variables with const. When you declare variables with const, you’ll see the const keyword. It is a good indicator that the variable is created in the current scope; it will never change the external state.

const newVariable = 'some value'

If you need a if/else statement to determine what value your variable should take, consider using a ternary operator instead of an if/else statement.

const toy = gender === 'boy' ? 'toy car' : 'doll'

If you need three or more if statements, consider writing a function that uses an early return. This makes your code more declarative as a result.

const getGift = score => {
  if (score === 100) return 'Xbox'
  if (score > 75) return 'new phone'
  if (score > 50) return 'meal'
  return null
}

const gift = getGift(70)