0

I'm pretty new to coding and can't find out why this is outputting undefined:

var var0, list = [var0];
list[0] = true;
console.log(var0)
  • 1
    You never assign anything to `var0`. What do you expect it to be? – VLAZ Nov 01 '20 at 14:03
  • I'm assigning true to list[0] which from what I understand should be var0 – Harry Landesburg Nov 01 '20 at 14:06
  • No, `list[0]` is the first item of the list. You don't have pointers and references here, just values - `[var0]` just creates an array `[undefined]`, nothing more. There is no relation with the variable after the creation of `list`. – VLAZ Nov 01 '20 at 14:08
  • `list[0]` contains a copy of the value of `var0` variable. Assigning `true` to `list[0]` doesn't assigns the value to `var0` as well. `list[0] = true` will just overwrite the value at index 0 in `list`. – Yousaf Nov 01 '20 at 14:08
  • 2
    Does this answer your question? [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – VLAZ Nov 01 '20 at 14:08
  • @VLAZ No, I couldn't find what I was looking for there – Harry Landesburg Nov 01 '20 at 14:19
  • 2
    @HarryLandesburg why? It very clearly explains that it's not a pass-by-reference language, hence you cannot expect changing a variable to change a copy of it or vice versa. I'm not sure what other explanation you're looking for. – VLAZ Nov 01 '20 at 14:21

2 Answers2

1

On line 1 you copy the value of var0 into the array.

On line 2 you replace the value in the array.

This doesn't have any effect on var0. That is just a variable that used to have a copy of the same value. It is not a reference.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • That makes sense, but how then can I store var0 inside that array so that it's value can be modified? – Harry Landesburg Nov 01 '20 at 14:17
  • You can't. JavaScript doesn't have explicit references. – Quentin Nov 01 '20 at 14:18
  • 1
    @HarryLandesburg it's impossible. If you share [what your overall goal is](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), we can probably advise how to approach it. Right now, it sounds like you probably just want to store your values in an object. – VLAZ Nov 01 '20 at 14:23
  • Darn, I'd expect that to be possible :/ – Harry Landesburg Nov 01 '20 at 14:25
  • @VLAZ I was attempting to check which arrow key is pressed making the code shorter than with if statements: `window.addEventListener('keydown', KD); var left, up, right, down, list = [left, up, right, down]; function KD(event) { let LA = 0; for (a = 37; a !== 41; a++) { if (event.keyCode == a) { list[LA] = true; } LA++ } LA = 0; }` I guess I'll have to go back to using a bunch of if statements... – Harry Landesburg Nov 01 '20 at 14:33
  • @HarryLandesburg ...or use an object as I already said. – VLAZ Nov 01 '20 at 14:34
  • @VLAZ Objects can store variables then? I'm confused – Harry Landesburg Nov 01 '20 at 14:39
  • @HarryLandesburg not "store variables" but you can just use one as a central location to store related values. If you change `keys.left = true` in one place, it'd be visible everywhere. If you make another object with a mapping between key code and your name for the keys, you don't need many `if`s just `key[mapping[event.keyCode]] = true` You can throw an `if` for when something doesn't exist in `mapping`, perhaps but otherwise you don't have much code to note down what was pressed. – VLAZ Nov 01 '20 at 14:43
1

You never use or define the value of var0.

list[0] = true;

This line replaces the value of the object in the 0 position (which is var0 because of the first line) to a boolean variable with the value "true".

What you mean to do is

var var0 = true, list = [var0];
console.log(list[0])
St.Nicholas
  • 104
  • 5
  • I know I could assign it true already but I'm trying to store it inside an array then set the first variable to true – Harry Landesburg Nov 01 '20 at 14:22
  • @HarryLandesburg - you can't because that's not how javascript works. You never pass a variable, just the value of the variable. – St.Nicholas Nov 01 '20 at 14:22