0

I have some simple code as shown below. Unfortunately, it fails on the second destructuring assignment and I am unable to figure out why.

Code:

let prev, curr

[prev, curr] = setPreviousAndCurrent(1, prev, curr)
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

[prev, curr] = setPreviousAndCurrent(2, prev, curr) // fails on this assignment
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

[prev, curr] = setPreviousAndCurrent(3, prev, curr)
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

function setPreviousAndCurrent(data, previous, current) {
    // We cannot use the ! operator for checking against values of previous and current since value can be 0, which will give a false positive
    if (previous === undefined) {
        previous = data
    } else if (previous && current === undefined) {
        current = data
    } else {
        previous = current
        current = data
    }

    return [previous, current]
}

Output:

❯ node js_test.js       
Previous: 1
Current undefined

/js_test.js:7
[prev, curr] = setPreviousAndCurrent(2, prev, curr)
             ^

TypeError: Cannot set property 'undefined' of undefined
    at Object.<anonymous> (/js_test.js:7:14)
    at Module._compile (internal/modules/cjs/loader.js:971:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
    at Module.load (internal/modules/cjs/loader.js:822:32)
    at Function.Module._load (internal/modules/cjs/loader.js:730:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1051:12)
    at internal/main/run_main_module.js:16:11

If I change the code to (as shown below), it works as expected.

Code:

let prev, curr

[prev, curr] = setPreviousAndCurrent(1, prev, curr)
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

let temp = setPreviousAndCurrent(2, prev, curr)
prev = temp[0]
curr = temp[1]
// [prev, curr] = setPreviousAndCurrent(2, prev, curr)
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

temp = setPreviousAndCurrent(3, prev, curr)
prev = temp[0]
curr = temp[1]
// [prev, curr] = setPreviousAndCurrent(3, prev, curr)
console.log(`Previous: ${prev}`)
console.log(`Current ${curr}\n`)

function setPreviousAndCurrent(data, previous, current) {
    // We cannot use the ! operator for checking against values of previous and current since value can be 0, which will give a false positive
    if (previous === undefined) {
        previous = data
    } else if (previous && current === undefined) {
        current = data
    } else {
        previous = current
        current = data
    }

    return [previous, current]
}
// Works as expected

Output:

❯ node js_test.js
Previous: 1
Current undefined

Previous: 1
Current 2

Previous: 2
Current 3

I am unable to figure out why this happens since it is clear that the function is running and returning the expected value but is unable to assign it via destructuring to the variables prev and curr, the second time it is called.

Appreciate the responses! Thank You! :)

philosopher
  • 1,079
  • 2
  • 16
  • 29
  • 1
    You are missing semicolons everywhere – Bergi Dec 07 '19 at 22:27
  • @Bergi Isn't it acceptable practise to write JS without semi-colons now? – philosopher Dec 07 '19 at 22:40
  • 2
    Yes, but only if you know [*where* you don't need them](http://inimino.org/~inimino/blog/javascript_semicolons). – Bergi Dec 07 '19 at 22:42
  • @Bergi Thanks! This solved the problem after reading the linked answer about ASI. Now I'm wondering if I should restart using semi-colons lol. Any opinions? – philosopher Dec 07 '19 at 22:43
  • Now that you've learned how to do it correctly you should be able to keep omitting them where possible. It's entirely opinion-based, make an informed decision yourself. Both are fine. Only thing I'd recommend is to get a linter that will watch over your chosen style. – Bergi Dec 07 '19 at 22:50
  • Thank you! I didn't know what a linter was until now so I will definitely get one. – philosopher Dec 07 '19 at 22:54
  • @Bergi wow ! I use JS for 1 year now and never knew that semicolons were required, I only put them because it's an habit I took from C. When I got my error I didn't suspect I forgot one semicolons and I wouldn't have thought about it because I thought it was optional. But yes, same problem, same solution for me. Do you know where I can find any explaination ? The link you gave is not working anymore – FranckEl Apr 21 '21 at 19:04
  • @FranckEl That's a real pity, it was the most exhaustive explanation of the topic I've seen. You can still read it under https://web.archive.org/web/20201206065632/http://inimino.org/~inimino/blog/javascript_semicolons. It's covered [elsewhere](https://stackoverflow.com/q/2846283/1048572) as well, though. – Bergi Apr 22 '21 at 01:58

0 Answers0