30 Mins

2. Modern JavaScript for React

Variables and Scope in the ES6 Era

In modern React development, we have moved away from the var keyword entirely. Understanding why is critical for avoiding memory leaks and scope-related bugs in your src files. When you define a variable with var, it is function-scoped or globally scoped, meaning it ignores block boundaries like if statements or for loops. This leads to hoisting issues where the variable is initialized to undefined before the execution flow reaches its declaration. Conversely, let and const introduce true block scoping, where variables are confined to the exact pair of curly braces {} where they were defined.

React (TSX)
const processUserData = (user) => {
  if (user.isActive) {
    const status = 'Active';
    console.log(status);
  }
  // status is not accessible here
};

In this snippet, const ensures status is garbage collected once the block finishes. const creates a read-only reference to a value, which is perfect for React components that shouldn't re-assign their internal logic props. For cases where values must change, such as local counters or toggle flags, let is the correct tool. By using these rigorously, the JavaScript engine (V8) can optimize memory allocation more efficiently, ensuring your UI remains snappy during re-renders.

Arrow Functions and Lexical Binding

Arrow functions are the bread and butter of React functional components. They offer a more concise syntax and, more importantly, lexical this binding. In traditional function declarations, this is determined dynamically based on how the function is called. In contrast, arrow functions capture the this value of the surrounding lexical context. This solves the classic "lost context" problem often encountered when passing methods as event handlers in class-based components, though in functional components, it primarily serves to keep your code clean and declarative.

SyntaxDescription
Arrow FunctionLexical this binding, concise syntax.
Concise ReturnImplicit return if only one expression.
React (TSX)
const UserGreeting = ({ name }) => {
  return <h1>Welcome, {name}!</h1>;
};

The code above uses an implicit return. Since there are no curly braces after the => arrow, the JavaScript engine automatically treats the expression

...

as the return value. This is highly standard in React for creating presentational components quickly.

Destructuring and State Immutability

Destructuring is a syntax shortcut that allows you to extract properties from objects or items from arrays and assign them to variables in a single statement. In React, we use this constantly to pull data out of props. When dealing with state, we rely on the Spread Operator (...) to maintain immutability. Because React triggers re-renders based on shallow comparisons, you must never mutate a state object directly. Instead, you create a new reference by spreading existing values into a new structure.

React (TSX)
const updateProfile = (prevUser, newEmail) => {
  return {...prevUser,
    email: newEmail
  };
};

Line-by-line: The spread operator ...prevUser copies every key-value pair from the old object into a new one. The email: newEmail line then overrides the specific email property. This ensures that the original prevUser object remains untouched, satisfying React's core requirement for immutable state updates.

Knowledge Check

Ready to test your understanding of 2. Modern JavaScript for React?

Back