A responsive page can usually let CSS calculate its own widths and font sizes. When a layout must react to user input, incoming data, or more complex conditions, JavaScript can calculate values in the browser and update the page as conditions change. section 1.3.3 section 6.5.7
JavaScript supplies several practical tools for that work:
- Arithmetic operators calculate dimensions, positions, and animation values. The Math object adds rounding, random numbers, extrema, square roots, and trigonometric functions. section 1.3.2 section 2.1.1
- Explicit comparisons such as >= and < let conditions select behavior. For example, window.innerWidth >= 768 tests whether a viewport has reached a layout breakpoint. section 1.4.6
- DOM updates and event responses can rearrange components or adjust sizes when CSS rules alone cannot express the required behavior. section 6.5.7
Divide dashboard space by weight
Suppose dashboard widgets arrive with weights that are not known until their data arrives. JavaScript can create the widget elements, read the container width and gap, add the weights, and calculate each widget’s share of usable horizontal space. CSS still supplies the flex layout and gap; JavaScript handles the values that depend on runtime data. section 6.5.7
let x = 5;
let y = x++;
Watch the order of operations: with let y = x++, y receives the old value, 5, before x becomes 6. Moving ++ before x increments first. section 2.1.1
Use CSS math when the browser can determine layout, spacing, or typography from style rules. Reach for JavaScript when calculations depend on interaction, runtime data, or complex decisions—and check numeric behavior when results are surprising. section 1.3.3 section 2.0











