You can explicitly control the image used by Facebook by implementing OpenGraph markup on your page. That way Facebook won't have to "guess" or "choose one" by itself.
Here is a Facebook document about it:
https://developers.facebook.com/docs/sharing/webmasters/
More specifically, you will need to add the following tag to your page head
to specify the image of the web page poster:
To answer your follow-up question, it is impossible to use JavaScript to modify the image that is displayed when someone sticks your link in Facebook, because the crawler of Facebook does not analyze JavaScript.
Sources:
https://stackoverflow.com/questions/44241258/set-dynamic-meta-tags-and-open-graph-tags-using-jquery
https://stackoverflow.com/questions/3081004/update-facebook-open-graph-meta-tag-through-javascript
You can however create a share button on your page which shares using a custom image. Note that in this case the image will only be defined if the user click on your custom share button. If they paste the link in Facebook to share, they'll just get the default image.
As far as I know, static or server-side opengraph tags are the only way to explicitly define an image when a URL is pasted into Facebook to share.