DEV Community

Cover image for 🔐🧩 Using let, const, and var in Your Code Development
João Victor
João Victor

Posted on

🔐🧩 Using let, const, and var in Your Code Development

This document presents fundamental guidelines for the effective use of let, const, and var in React projects with TypeScript. The central purpose is to establish clearer, more resilient, and efficient code, emphasizing the preference for let and const while discouraging the use of var. These practices aim to promote immutability whenever feasible, ensure code readability, and strengthen security, minimizing potential errors during development. For more insights and to explore my other repositories or access this post in Portuguese, be sure to visit my GitHub profile at my GitHub.

Usage Guidelines

  • const: Use to declare immutable values. Prefer it whenever possible to enhance readability and promote immutability in the code. Example: values that will not be reassigned, such as constants, strings, arrays, and immutable objects.
  • let: Use for variables that need to be reassigned during program execution. Avoid excessive use to ensure clarity and maintain controlled mutability. Example: variables that will change over time.
  • var: Avoid using var due to its function scope, hoisting issues, and lack of block scope support. Prioritize let and const over var to ensure more predictable code and avoid potential bugs.

Practical Example

// Example of using const and let in a React component:

import React, { useState, useEffect } from 'react';

const MyComponent: React.FC = () => {
  const [counter, setCounter] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      // Using let to allow reassignment
      let newCounter = counter + 1;
      setCounter(newCounter);
    }, 1000);

    return () => clearInterval(interval);
  }, [counter]);

  return <div>The counter is at: {counter}</div>;
};
export default MyComponent;

Enter fullscreen mode Exit fullscreen mode

Immutability of Objects and Arrays Declared with const

Although using const ensures that the reference to objects and arrays cannot be reassigned, it's crucial to understand that the internal content of these data types remains mutable. This means that even when declared with const, objects and arrays can still have their properties and elements modified. To prevent unwanted modifications, especially in contexts where immutability is essential for code security, consider using Object.freeze() to freeze the object or array, making its content completely immutable. This approach reinforces the integrity and predictability of the code in React projects with TypeScript.

  • Example with Object:
const myObject = { name: "John" };
// This is allowed
myObject.name = "Mary"; 
// Output: "Mary"
console.log(myObject.name); 
Enter fullscreen mode Exit fullscreen mode
  • Example with Object.freeze:
const myObject = Object.freeze({ name: "John" });
// Does nothing, as the object is frozen
myObject.name = "Mary"; 
// Output: "John"
console.log(myObject.name); 
Enter fullscreen mode Exit fullscreen mode

Conclusion

The appropriate use of let and const in React projects with TypeScript is essential to promoting clearer, more robust, and efficient code.

  • const promotes immutability and should be preferred whenever possible.
  • let is used for mutable variables in a controlled manner, avoiding excessive use.
  • var is discouraged due to scope and hoisting issues and should be replaced by let or const.
  • Additionally, using Object.freeze() can ensure complete immutability of objects and arrays, further enhancing the security and predictability of your code.

References

Top comments (0)