Why do booleans behave differently in module js vs common js?

Viewed 30

I have a quick script that I initially wrote in common.js and when it was finished I wanted to write a driver script showing the function and changed the type to module.js.

The script traverses a graph to find the shortest path and I keep track of visited nodes using an array, with elements set to true/false if it was visited.

The values for every visit are set to false:

  let visit = new Array(9);

  for (let i = 0; i <= 8; i++) {
    visit[i] = new Array(9); 
    for (let j = 0; j <= 8; j++) {
      visit[i][j] = false;
    }
  }

The root node visit is changed to true:

visit[(rootNode[0], rootNode[1])] = true;

Then as the script progresses, touched nodes are changed to true which is where the error happens.

visit[x][y] = true;

Running the script in common.js gives the correct output. Running the script in module.js throws an error, saying:

TypeError: Cannot create property '1' on boolean 'true'

Where 1 is the x value of the visited node.

Why is the behavior different?

Edit: The issue was with ES6 Modules requiring strict mode. My typo with the parenthesis worked perfectly fine in CommonJS on the edge cases I was testing but not on the regular tests. Fixing the typo made it work everywhere, thanks for the help.

0 Answers
Related