How can I add JavaScript only on the front page? I created a custom Theme: filhos_do_porto using Bartik as a subtheme and I would like to add JS code only to the Front Page.

Can someone help me? (I didn’t find anything specific of D8 clear enough for someone new in Drupal)
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.