Google showing static title instead of dynamically set javascript title

Let’s say I have set my title using HTML to “Something – Example” and with Javascript I change the title to “Apple – Example”.
When I google my website (after waiting for it to update) or the link gets embedded on websites like Twitter or Discord, it shows the static “Something – Example” title instead of the new title.