How can I add JavaScript only on the front page?
One way, in your mytheme.theme file:

/**
 * Implements hook_page_attachments_alter().
 */
function mytheme_page_attachments_alter(&amp;$attachments) {
  if (\Drupal::service('path.matcher')-&gt;isFrontPage()) {
    $attachments['#attached']['library'][] = 'mytheme/library-name';
  }
}


This points to an entry in your mytheme.libraries.yml file which list out what file(s) to load.