Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
Uncaught TypeError: Assignment to constant variable

My variable is not a constant and I still get an error: "Uncaught TypeError: Assignment to constant variable." Any idea why?

snake.js

export let SNAKE_SPEED = 3;

food.js

import { snakeBody, SNAKE_SPEED } from "./snake.js";

export function update() {

  let head = snakeBody[0];
  if (head.x == food.x && head.y == food.y) {
    food.x = Math.round(Math.random() * 21);
    food.y = Math.round(Math.random() * 21);
    SNAKE_SPEED++;
  }

}

The variable in question being SNAKE_SPEED.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Identifiers imported from other modules cannot be reassigned. To achieve something like this, you can have the other module export a function that changes it, eg:

export let SNAKE_SPEED = 3;
export const changeSnakeSpeed = newSpeed => SNAKE_SPEED = newSpeed;
import { snakeBody, SNAKE_SPEED, changeSnakeSpeed } from "./snake.js";

and then call changeSnakeSpeed(SNAKE_SPEED + 1), and SNAKE_SPEED will have changed.

Or do something like

export const incrementSnakeSpeed = () => SNAKE_SPEED++;
incrementSnakeSpeed();

Or put the often-changeable variables into a single object that can be mutated (or reassigned and retrieved again, if you prefer immutability).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda