Pass a dynamic variable to a javascript function

How Do I Pass a dynamic variable to a javascript function? I want to be able to click a button run a database query get a result and pass it to a javascript function to do something

Maybe this link could be help to you