heliu heliu - 1 year ago 52
Javascript Question

How can i make a function that takes in a parameter and when executed, it alerts whatever you give the function? (javascript)

I am in a coding bootcamp prep class and I did not pass my first quiz because i did not understand functions. I talked to my mentor about the problem that get me stuck, he said it does not remember the problem word by word but it was

"It is basically writing a function that takes a parameter and when executed it alerts whatever you give the function"

I guess what got my stuck was when I writing the function when I got to the part

var varName = function(parameter){ alert()}

I was not too sure what to put in the alert(), I keep thinking it was suppose to take in a string so when it actually alerts, it will display something a sentence to tell the user.

Answer Source

So in basics a function is a set of code that can be re-used and executed by calling the function directly.

For simplicity sakes alert() is a function and has the parameter message.


Window is optional as the alert method is part of the window scope.

So to create a function that takes a paramater can be a simple as

function myAlert(message) {

myAlert('test message');

The first block defines the function as seen by function xx(...). The function when executed accepts the paramter message. Then this message is simply passed to the alert(...) function.