OMRY VOLK
OMRY VOLK

Reputation: 1481

Solving a JavaScript error which appears only in safari

In my JS code I have the following function:

function clear_results(flow = false) {
  //do some stuff
}

This works fine on Chrome and Firefox but raises an error on Safari:

SyntaxError: Expected token ')'

Changing to:

function clear_results(flow) {
  //do some stuff
}

Fixes the problem but I want flow to have a default value of false if not given. Thanks

Upvotes: 1

Views: 1172

Answers (3)

Alex
Alex

Reputation: 161

The way you are setting the default parameter appeared in ES6 and is currently not supported by Safari. You can look at http://kangax.github.io/compat-table/es6/ at the row "default function parameters" to see what browsers are supporting this feature.

Here is

function clear_results(flow) {
    flow = flow || false;
    //do some stuff
}

or (according to Set a default parameter value for a JavaScript function)

function clear_results(flow) {
    flow = typeof flow !== 'undefined' ? flow : false;
    //do some stuff
}

Upvotes: 2

Sreejith Sasidharan
Sreejith Sasidharan

Reputation: 1378

Check it inside the function

flow = typeof flow !== 'undefined' ? flow : false;

Upvotes: 0

techfoobar
techfoobar

Reputation: 66693

The correct way to solve this in a way that works across all browsers is.

function clear_results(flow) {
  if(typeof flow === 'undefined') {
    flow = false;
  }
  //do some stuff
}

Note that the syntax you used for specifying a default argument value isn't incorrect, but it'll be a while before that is supported across all browsers.

Upvotes: 1

Related Questions