I have the following:
var currentQuestion = $(this).closest(".question")
I have tried everything suggested in this, this and this question:
if(currentQuestion !== undefined)
if(currentQuestion !== "undefined")
if(typeof currentQuestion !== undefined)
if(typeof currentQuestion !== "undefined")
if(currentQuestion !== null)
if(currentQuestion != undefined)
if(currentQuestion != "undefined")
if(currentQuestion.data("index") !== null)
if(currentQuestion.data("index") !== undefined)
if(typeof currentQuestion.data("index") !== undefined)
But it keeps going inside the if statement...
I have this inside the if:
console.log("nextQ: " + currentQuestion.data("index"));
and nextQ: undefined is getting print out
any other ideas?
EDIT:
currentQuestion.data("index") != null
worked out. If you check all the options I tried before, the one similar to this one had this comparison element: !== and not !=. That change made the difference. If someone can explain why, I'll grant him/her the correct answer.
console.log(currentQuestion.data("index"), currentQuestion);PS: just a side note: real computer science engineers don't make random things and expect them to work.undefinedvalues isn't true - because it is defined and stores an object.!== nulldoesn't work but!= nulldoes, is that the value returned is notnull. When there is no data associated with the element, thedatamethod returnsundefined. Comparing that tonullusing the!==operator will givetrue, asnullandundefinedis not the same type. Comparing them using the!=operator will givefalseas they are converted to a type that can be compared value to value, and the conversion will make them end up as the same value. To check if there is no data associated with the element, check the type that thedatamethod returns.