0

I am using Javascript in Firefox (version 77), and I have a few console.log statements to observe some values that are not being set correctly. If I view the variables they somehow get fixed, but then I get an error! If I don't view them, the program runs without a hitch, but gets the values wrong!

The program was large, but after reducing to MWE, it is extremely simple. Here it is:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript">
        function draw() {
            function quad(A, B) {
                console.log(A, B)
                return [1,2,3]
            }

            let A = [5, 5]
            let B = [105, 5]
            let [C, D, E] = quad(A, B)

            A = [110, 5]
            B = [210, 5]
            console.log(B)      // This is line 18
            [C, D, E] = quad(A, B)
            console.log(C,D,E)
        }
    </script>
</head>

<body onload="draw();">
</body>
</html>

The behavior is as follows:

If I run it as it is, I get the expected output, and then it has an error. The output is

Array [5, 5 ]  Array [ 105, 5 ]
Array [210, 5]
Array [110, 5]  Array [ 210, 5 ]

and the error is

Line 19:16 has error: TypeError: console.log(...) is undefined

On the other hand, if I comment line 18, I get no error, but the wrong output

Array [5, 5 ]  Array [ 105, 5 ]
Array [110, 5]  Array [ 105, 5 ]
1 2 3

The second array in the middle line should be [210, 5], because that is what it was set to just moments ago.

It appears that it has something to do with the multiple assignments, because when I replaced C,D,E with a single variable, then it started to work.

Roobie Nuby
  • 1,109
  • 10
  • 17

0 Answers0