Considering text as an array variable, please explain the push method statement shown below:
var text = [];
text.push(document.getElementById('name').innerHTML || 0);
Considering text as an array variable, please explain the push method statement shown below:
var text = [];
text.push(document.getElementById('name').innerHTML || 0);
The trick is that an empty string is evaluated as false in boolean tests like this.
Example :
var a = "" || 0; // a is set to 0 because "" is empty and is false
var b = "non-empty" || 0 // b is set to "non-empty" because it's not an empty string
it will add HTML content of dom element with id='name' if it's not empty to text array , otherwise it will add 0 to it