Am I required to call a Parameter if I determine it in a function?

0

Hello, I'm having a slight doubt about this if I determine a parameter in creating the javascript function, am I required to call it? example:

    function ts(metodo){
     //código aqui
   }

Can I give an onclick without determining that parameter? example:

<span onclick="ts();"> ts </span>

Or am I forced to call the Parameter together?

And if not how do I check if the Parameter was sent type the isset of php?

    
asked by anonymous 20.08.2018 / 16:36

1 answer

2

You do not have to pass it, but the function needs to be handled in case the parameter is not set, otherwise it will not make sense to do so.

For example, we assume a function that displays a greeting message on the console. The function will receive the name to which it will be greeted:

function hello(name) {
  console.log('Olá, ${name}')
}
<button onclick="hello('mandioca')">Saudar</button>

But, look what happens when you do not set the parameter:

function hello(name) {
  console.log('Olá, ${name}')
}
<button onclick="hello()">Saudar</button>

The function is executed, but an "undefined" appears because the parameter has not been defined.

In this case, if it really makes sense to call the function like this, you can assign a default value when the parameter is not set:

function hello(name) {
  name = name || 'mandioca'  // Se não tiver definido, atribui o valor 'mandioca'
  console.log('Olá, ${name}')
}
<button onclick="hello()">Saudar</button>

So when you call the function without a parameter, the function will assign a default value and continue execution.

    
20.08.2018 / 16:47