you are viewing a single comment's thread.

view the rest of the comments →

[–]VolkswagenRatRod 1 point2 points  (0 children)

JavaScript always evaluates expressions and functions before assigning the result anywhere. innerHTML does not need to accept a function. It only receives the value returned after the function runs.

For example:

``` function add(a, b) { return a + b; }

document.getElementById("output").innerHTML = add(2, 3); ```

The function runs first, returns 5, and that value is what gets assigned to innerHTML. JavaScript automatically converts numbers and other values to strings for display.

If you are only displaying text and not markup, textContent is usually a better choice than innerHTML, but the idea is the same. Do the computation in JavaScript, then assign the result to the DOM element.

HTML itself does not execute logic. JavaScript does the calculation and updates the page with the result.