Rahul Gupta
Rahul Gupta

Reputation: 994

'Syntax error, unexpected token =' in js function

I am getting this error in chrome while mozilla is handling good. I am getting this error to a function which is like this

function abc( xyz = false){ "My logic" }

Error is pointing to '=' operator. please help with this.

Upvotes: 1

Views: 5245

Answers (2)

Jai
Jai

Reputation: 74738

That is a standard of ECMASCRIPT version 6 and it's called Default parameters. So it might be not available in your chrome version while FF has.

You can achieve the same by two ways:

function abc( xyz ){ "My logic" }

var pVal = mightbe || false;
abc(pVal); //<---- now pass it here;

or:

function abc( xyz ){ 
    // before processing anything you can do this
    var o = xyz || false; // if there is any value in the xyz then that will
                          // be assigned otherwise false will be the default value.
    "My logic" 
}

Upvotes: 4

Natural Lam
Natural Lam

Reputation: 742

This is ES6 syntax, most browsers only support very few ES6 features, you can check from here: https://kangax.github.io/compat-table/es6/ (In your example you used default function parameters)

If you want to write ES6 syntax (which is quite appealing in many ways), you can use some code transpiling tool like babel: https://babeljs.io/

Upvotes: 0

Related Questions