Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I don't pass a parameter in a Javascript function?

I am new to the world of Javascript and am tinkering with writing very basic functions and stumbled upon the example below by accident and am unsure why it works when I am not passing a parameter when the function demands it.

Sample function

function myfunction(x) {     alert("This is a sample alert"); } 

Now if I call the function myfunction(); I am presented with the alert. Why is that that I am able to call the function without any errors or warnings when I have not passed a parameter?

EDIT

I did not expect so many great answers and I am by no means in a position yet able to say which answer is the best so am I able to request people to suggest the best answer and I'll award the acceptance to that person.

like image 864
PeanutsMonkey Avatar asked Jun 19 '12 19:06

PeanutsMonkey


People also ask

What happens if you don't pass any parameter in the JavaScript function?

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional. All the parameters that weren't "supplied" will have the undefined value.

Can a JavaScript function have no parameters?

The functions have different structures such as parameters or no parameters and some function return values and some do not in JavaScript. The simplest of function is the one without an parameters and without return. The function compute its statements and then directly output the results.

Why do we need to pass parameters?

Parameters are essential to functions, because otherwise you can't give the function-machine an input.

Can I call a function without parameter?

You can use a default argument in Python if you wish to call your function without passing parameters. The function parameter takes the default value if the parameter is not supplied during the function call.


1 Answers

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional.
All the parameters that weren't "supplied" will have the undefined value.

function foo(x, y, z){     //... }  foo(1); 

Inside the foo function now:

function foo(x, y, z){     x === 1     y === undefined     z === undefined } 

You can even pass more arguments, like:

foo(1,2,3,4,5,7); // Valid! 

You can know the amounts of parameters supplied by arguments.length from inside the function.

function foo(x, y, z) {      console.log('x value: ' + x);      console.log('y value: ' + y);      console.log('z value: ' + z);      console.log('Arguments length: ' + arguments.length);  }  console.log('Zero parameters');  foo();  console.log('Four parameters');  foo(1, 2, 3, 4);

Example of useful function that handle any amount of parameters:

function max() {      var maxValue = arguments[0];      for (var i = 1; i < arguments.length; i++) {          if (maxValue < arguments[i]) {              maxValue = arguments[i];          }      }      return maxValue;  }    alert(max(1, 5, 7, 2, 88, 32, 44));
like image 110
gdoron is supporting Monica Avatar answered Sep 27 '22 19:09

gdoron is supporting Monica