How can I access environment variables from a javascript file in Craft 3?
The .env file is on the server and javascript is running in the client so you need to pass it through to javascript in your twig template inside a script tag.

{% set env = getenv('ENVIRONMENT') %}
&lt;script&gt;
    var myVariable = '{{ env|e('js') }}'
&lt;/script&gt;


You probably don't need to escape it like in my example above, but good practice anyway. If you are then using the variable in that script you are good to go. From elsewhere on the page you can get the variable using:

document.addEventListener('DOMContentLoaded', () =&gt; {
    const myVar = window.myVariable;
});


Great article about this and more https://cruftlesscraft.com/passing-data-from-twig-to-javascript