The very basic consept, is that you create a function and put the javascript code that you want to execute
function fuctionName(){
(In here)
}
You then can create a button by creatng a button code, and inside the onclick = '' part, you add functionName() inside the ''. What this does is it executes the code inside the function every time the button is clicked by referencing and putting the functionName() code into action.