0

Javascript array value is undefined ... how do I test for that

and

How to check a not-defined variable in JavaScript

these are wrong as far as I'm concerned :

I ONLY get : error

when trying to :

console.log(!fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7].value.firstInput);
console.log(fromToParameters[7].value.firstInput === undefined);
console.log(typeof fromToParameters[7].value.firstInput == 'undefined');
console.log(fromToParameters[7].value.firstInput !== undefined);
console.log(typeof fromToParameters[7].value.firstInput != 'undefined');

but this (the entry exists) works fine :

console.log(!fromToParameters[0].value.firstInput);
console.log(!!fromToParameters[0].value.firstInput);
console.log(fromToParameters[0].value.firstInput === undefined);
console.log(typeof fromToParameters[0].value.firstInput == 'undefined');
console.log(fromToParameters[0].value.firstInput !== undefined);
console.log(typeof fromToParameters[0].value.firstInput != 'undefined');

false true false true false true

is it a question of react being different from js? why can't I do the same as in these stackoverflow threads?

UPDATE :

so you cannot point to a missing array element at all.

Check answers below.

I think I'll be using an array.lenght stored in a const that I then check my increment against it within my "for" loop to allow or disallow modifying my array entries on a case-by-case basis.

it's really annoying that you can't just ask js if a damn var of an unexisting array index exists or not.

it seems like this would be straightforward stuff : can't point to the index? then NO. no not this variable nor any other variable exists at this index. end of.

the guys at js definitely should put in a note for adding something as simple as this.

I'm tempted to post my code as I have something that allows for me to do what I want (calling a index with lots of undefineds and getting a object with ""s instead) but it's a bit monstrous.

tatsu
  • 2,316
  • 7
  • 43
  • 87
  • You sure the size of `fromToParameters` is at least 8? – Makoto Sep 12 '17 at 15:47
  • it's not it's under. how do I test a variable value for an array pointer that doesn't exist? that's what I need to have. – tatsu Sep 12 '17 at 15:49
  • Wait. Wait wait wait. You can't index into position 7 if `fromToParameters` is not at least 8 entities long. You literally *can't* do it. What ***specifically*** are you trying to test for? – Makoto Sep 12 '17 at 15:50
  • these variables are going to exist and need to be filled with " " if they do not exist AKA if the index doesn't go that high. how do I test for that ? – tatsu Sep 12 '17 at 15:52

2 Answers2

1

Check for the index first (ex: console.log(!yourArray[x]), then depending if that test passes or fails, access/add the indexes/properties you want.

connected_user
  • 814
  • 1
  • 9
  • 25
  • that doesn't work either that just always returns true no matter what number on the array I feed it. – tatsu Sep 12 '17 at 16:22
  • It should return false if the index exists and return true if the index doesn't exist. – connected_user Sep 12 '17 at 17:17
  • well it doesn't. are you testing this in a reactjs app? – tatsu Sep 13 '17 at 07:12
  • Yes this is in a react app. How are you testing it? Are you testing 'fromToParameters[7]' or 'fromToParameters[7].value.firstInput'. Before you access any of the properties of 'fromToParameters[7]', just use the index ('fromToParameters[7]') without accessing any properties to check if the index is valid. – connected_user Sep 13 '17 at 13:00
  • Oh! my bad you're absolutely right with just `console.log(!fromToParameters[7]);` it works – tatsu Sep 13 '17 at 13:24
1

You should do like this:

console.log(fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log( fromToParameters[7]&& typeof fromToParameters[7].value.firstInput == 'undefined');

I have just added check. So if fromToParameters[7] is undefined or null, your code will not break.

Ved
  • 11,837
  • 5
  • 42
  • 60
  • it does break though. I don't know what you're testing in but it's not reactjs. in anycase I'm going to be using `if`s on the `length` of my arrays to allow or not my attribution of variable within my `for` loop. it should do the job except that means I account nowhere for the variables somehow ending up as `undefined`. there's apparently no way to handle that. – tatsu Sep 12 '17 at 16:19
  • @tatsu I have updated my answer. removed leading `!`. And what do you mean by ` it's not reactjs`. Is reacts not javascript? – Ved Sep 12 '17 at 16:22
  • ok. that's actually good : first returns `undefined` second returns `false` ( : > finally ) and last one breaks: `Cannot read property 'value' of undefined`. – tatsu Sep 12 '17 at 16:25
  • @tatsu you are correct. I have updated my the answer. – Ved Sep 12 '17 at 16:29
  • no. it's returning `undefined`. as it should, right? – tatsu Sep 13 '17 at 07:09