Home > other >  How come while (myNode.lastElementChild) works without any value?
How come while (myNode.lastElementChild) works without any value?

Time:01-17

Im learning JS by analyzing a to-do list tutorial and I'm stuck trying to fully understand the functions that clears a populated list by clicking a button. I think I understand this small script but not sure.

    `function clearTaks(){
        while(taskList.firstChild){
            taskList.removeChild(taskList.firstChild);
        }`

I think that the condition while(taskList.firstChild) means that meanwhile taskList still has a Chidren, keep doing... but I wonder why that works without any other value. I mean, shouldn´t it be something like this?: while(taskList.firstChild != false) or something similar. Thanks in advance for your help

CodePudding user response:

taskList.firstChild is coerced to a boolean.

firstChild will return null if there is no child, which is Falsy (thus you will get false and the loop will terminate).

  •  Tags:  
  • Related