Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!