Javascript is an Event driven scripting language.
Javascript programs are only executed in response to some Event.
An Event could be a mouse click, a page load, a button being clicked etc.
You decide which events are used to trigger which pieces of code.
Javascript is linked to web pages in three main ways,
You can even use all three at the same time.
It is fairly widely accepted that external js files are the best way to
add javascript to a page.
Using external js files helps with the concept of separating content
from behaviour.