0

I have a function:

function modifySpinner(currentIcon, selector, spinType = remove)
{
    if (spinType === 'add') {
        // something
    }
}

But I am receiving this error in my console:

Uncaught SyntaxError: Unexpected token =

This wasn't causing issues in firefox? But in chrome its not working.

3 Answers 3

1

Try this instead:

function modifySpinner(currentIcon, selector, spinType)
{
    var spinType = spinType || "remove" 

    if (spinType === 'add') {
        // something
    }
}

So why this works: if the spinType doesn't have a value it equates to undefined. With var spinType = spinType || "remove" you are saying "hey evaulate spinType and if it's false, then use remove." undefined and null both evaluate to false in this conditional statement so, it's shorthand for saying if this value is undefined, use this other value instead.

Truthy and Falsy in Javascript

Sign up to request clarification or add additional context in comments.

Comments

1

You can't use this syntax for a default value.

function modifySpinner(currentIcon, selector, spinType )
{
  spinType = spinType || "remove";
  if (spinType === 'add') {
      // something
  }
}

Like this, if it's undefined, null, ... The value will be remove

Comments

0

Javascript doesn't have default parameters (it's been introduced in Ecmascript 6), you have to do the check yourself:

function modifySpinner(currentIcon, selector, spinType)
{
    spinType = typeof spinType  !== 'undefined' ? spinType : 'remove';
    if (spinType === 'add') {
        // something
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.