Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Uncaught TypeError: Cannot read property '0' of undefined

I know there's plenty of questions related to this error and I've checked most of them and none help me solve my issue. (Which seems so easy to debug...)

I have an array (which is empty aat first):

var words = [];

And my function hasLetter, checks if we find a letter (object) in the array (that I call here: d) words.

function hasLetter(letter,d){

// if words[0] not null should return object of letter "a", here we getting
// the index of the letter (since ascii of "a" is 97, I substract 97)
var ascii = letter.charCodeAt(0)-97;

//Trying to not get an error with this but still creates an err
if(typeof d[ascii ] !== "undefined" && d[ascii ] !== null && d[ascii ].length > 0){
    if(d[ascii].letter == letter){
        return true;
    }
}
return false; }

and I have a function called addLetter which checks if hasLetter returns true/false and then creates or not accordingly a new node.

function addLetter(letter,d){
var ascii = letter.charCodeAt(0)-97;
if(!hasLetter(letter,d)){
    document.write("This letter" + letter + " hasn't been found in words.");
    d[ascii] = new Node(letter);
}
    document.write("This letter " + letter + " already exists in words.");
    document.write(d[ascii].letter);

}

and if I test:

addLetter("a",words);

it returns:

Uncaught TypeError: Cannot read property '0' of undefined

I don't know what to do to say "if it's undefined then don't look into it or something along those lines...

Thanks

like image 707
Greg Uptron Avatar asked Apr 03 '15 11:04

Greg Uptron


People also ask

How do you solve TypeError Cannot read properties of undefined?

To solve the "Cannot read properties of undefined" error, use the optional chaining operator to check if the variable is not nullish before accessing it, e.g. person?. name . If the variable is undefined or null , the operator short-circuits instead of throwing an error.

What does Cannot read property of undefined mean?

The “cannot read property of undefined” error occurs when you attempt to access a property of a variable that is undefined . For example: const obj = undefined; // TypeError: Cannot read properties of undefined (reading 'prop') console. log(obj.

Can not read the property of undefined jquery?

Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined .

Can not read the property of null?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.


2 Answers

The error is here:

hasLetter("a",words[]);

You are passing the first item of words, instead of the array.

Instead, pass the array to the function:

hasLetter("a",words);

Problem solved!


Here's a breakdown of what the problem was:

I'm guessing in your browser (chrome throws a different error), words[] == words[0], so when you call hasLetter("a",words[]);, you are actually calling hasLetter("a",words[0]);. So, in essence, you are passing the first item of words to your function, not the array as a whole.

Of course, because words is just an empty array, words[0] is undefined. Therefore, your function call is actually:

hasLetter("a", undefined);

which means that, when you try to access d[ascii], you are actually trying to access undefined[0], hence the error.

like image 196
theonlygusti Avatar answered Oct 17 '22 15:10

theonlygusti


There is no error when I use your code,

but I am calling the hasLetter method like this:

hasLetter("a",words);
like image 29
Akash Chavda Avatar answered Oct 17 '22 14:10

Akash Chavda