Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda