1

I'm building a little click game with HTML, css and Javascript and I don't think this question belongs in the Game Dev section but here.

I've a a function to put a title and a text in a message box then it shows it and when you click on continue it will hide the message. I've given it 4 parameters,

function messageUser(title, text, fire, funct){

The first 2 are strings for Title and text, third a boolean to ask if the user would like to fire a function after the user has clicked on the button and the fourth is a string for the function name you would like to fire after you've clicked continue.

This comes in handy if you'd like to show a message before you'd like to show a character creation screen or something like that, but if I don't want to fire a function after the message can I leave the parameters empty when calling for the function like messageUser("Bra","Foo"); ?

Derk
  • 42
  • 8
  • 1
    All the parameters you leave empty will be automatically set to undefined. So if you check for the existence of funct before you call it, you can indeed just leave it empty. – Shilly Oct 20 '15 at 08:52
  • 5
    Maybe you're looking for this? http://stackoverflow.com/questions/148901/is-there-a-better-way-to-do-optional-function-parameters-in-javascript – Pablo Camara Oct 20 '15 at 08:53

2 Answers2

2

You can omit them. A missing parameter is undefined.

Magus
  • 14,796
  • 3
  • 36
  • 51
1

you can have a look at this-

It clearly says-

If a function is called with missing arguments (less than declared), the missing values are set to: undefined Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:

RajSharma
  • 1,941
  • 3
  • 21
  • 34