When not embedding them directly from the Masterpage you usually add them to a template using a Web Part. The 'Head HTML Code' Web Part is suited for this.
If you want to make an exception it's usually best to just create a different template for the other pages. But in case it's depending on some other aspect you could use the enabled setting of the 'Head HTML Code' Web Part.
- Add the 'Head HTML Code' Web Part to the Template and add your JS reference
- based on your requirement let the Web Part only be Enabled when a certain condition is met
Click the Arrow icon to open the popup where you can use a macro:
![Image Text](https://i.ibb.co/vhvdMBc/afbeelding.png)
e.g. When current document has a special boolean set to true or a certain field has a value.
Something like:
{% (bool)CurrentDocument.LoadScript %}
or
{% CurrentDocument.SpecialCategory == 'SpecialPage' %}
Can also be something completely different, but you didn't share the requirement.