missing ) after formal parameters (Javascript)


#1

Hello,

I’m having an error in Javascript, error is “missing ) after formal parameter”

This is the code:

init:function(variable1 = "", variable2 = "", variable3 = "")

Not sure where I’m going wrong here? My IDE isn’t giving a syntax error or anything else, I use IntelliJ Idea.

Thanks!


#2

looks like some pretty broken javascript code.

maybe provide the full context.

var foo = {
  init : function(variable1,variable2,variable3){
    // something
  }
};

#3

A parameter’s default value cannot be defined in the parameter list. Instead, you have to declare the parameter and then set its default value in the function body, like this:

init : function(variable1, variable2, variable3) {
    variable1 = variable1 || "";
    variable2 = variable2 || "";
    variable3 = variable3 || "";
}

This code uses a shorthand syntax to give each parameter a default value if a value was not passed to a parameter. It is functionally equivalent to the following:

init : function(variable1, variable2, variable3) {
    if (!variable1) {
        variable1 = "";
    }

    if (!variable2) {
        variable2 = "";
    }

    if (!variable3) {
        variable3 = "";
    }
}

#4

Hmmm, didn’t knew that in JS you can’t define a default value. In PHP it can, so I thought that it should work.

Thanks!