How can I access custom data fetched from posts in a javascript script?

The best practice way would be to create a REST API endpoint using register_rest_route() hooked to rest_api_init that your Javascript can query.

tech