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

Asked

Viewed 45 times

0

Hello am I with a slight doubt type if I determine a Parameter in creating the javascript function I am obliged 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 I am obliged to call the Parameter together?

And if not how to check if the Parameter was sent type php isset ?

  • 1

    You have a function that takes a parameter, but you don’t want to pass a parameter to the function? Is that it? If yes, it will depend on the code you have in "code here". If the function is not handled to not receive the parameter, it will not make sense to call it without it.

  • yes that , I knew it was called parameter rs. And also how do I check if something came in the parameter type if the parameter exists

1 answer

2


You are not required to pass it, but the function needs to be handled in case the parameter is not set, otherwise it makes no sense to do so.

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

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

But, see 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 a "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 the execution.

  • and the isset type mode of PHP type if(isset($ts)) for javascript?

  • @Cyberhacker didn’t understand what you meant

  • wanted to check whether the parameter was or n passed if it was running a part of Function if n it execulte another part

  • May user typeof name === 'undefined' to check when it was not passed.

  • in PHP would be like: if(isset($var)){}else{}

  • thanks gave here

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.