You’ll notice in their examples that their javascript is below any of the html elements that is utilized so that part of the dom is loaded before it starts executing the javascript.
Simply putting your javascript at the bottom of the body is usually good enough.