So i am pretty new with JavaScript, and it is realy not my cup of tea. We have ran into some issues in a large codebase with code that looks a bit like so:
var no = {somecontent:"text"};//Just any random object
document.write(no.length);
if(no.length != 0){
document.write("Something is in this list!");
}
if(typeof(no.length) == "undefined"){
document.write("OH GOD WHY?! THE LIES!");
}
Minus the last write, we just had the '!= 0' check for length of something which we assumed was an array. Since it is Undefined type, it simply returns true and assumed "something is in the list". This obviously caused a lot of issues, and we are afraid that there are a lot more of these errors in the codebase. (1500 lines by now)
So now my question is. Is there any sort of java parser/checker/compiler whichever that catches this in a form of an error or warning like: "You are trying to test for X while object/parameter is "undefined"!" ?
I imagine this must be a pretty common thing to happen, expecially when the programmer is inexperienced with the language. (Comming from Java/C#) But i couldn't find any answer on this besides adding: "typeof(no.length) == "undefined"" To every if statement in the code. Which seems like overkill and probably slams performance in the ground at runtime.
Any help with this issue is greatly appreciated!
Thanks in advance, Smileynator