You can set an HTML data-attribute that can be accessed by Javascript.
Take a look at the code example below where we are utilizing the HTML data-attribute that will be accessed in the script block.
Sightly.html
<section data-sly-use.taxcal="com.sourcedcode.core.components.models.TaxCalculator"
id="tax-calculator"
data-year="${taxcal.year @ context='scriptString'}"></section>
<script>
const article = document.querySelector('#tax-calculator');
console.log(article.dataset.year);
</script>
sourced: https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript