1

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

OMRY VOLK
  • 1,429
  • 11
  • 24

3 Answers3

2

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
}
Community
  • 1
  • 1
Alex
  • 151
  • 1
  • 6
1

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.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

Check it inside the function

flow = typeof flow !== 'undefined' ? flow : false;
Sreejith Sasidharan
  • 1,318
  • 5
  • 25
  • 46