Uncaught SyntaxError: Unexpected token = in Google Chrome

I have a javascript function which accept an optional parameter. This works fine in Firefox, but in Google Chrome it shows:-

Uncaught SyntaxError: Unexpected token =

My Code,

function errorNotification(text = "Something went wrong!") {
  $.pnotify({
      title: 'Error',
      text: text,
      type: 'error'
  });
}

I have seen lot of similar questions but I can't realize my problem.

Answers


You are using a default parameter feature which is supported only by Firefox now.

function errorNotification(text) {
    text = text || "Something went wrong!";
    $.pnotify({
        title: 'Error',
        text: text,
        type: 'error'
    });
}

Javascript does not allow you to pass default arguments like that. You need to assign the default internally to the function.

function errorNotification(text) {
  text || (text = "Something went wrong!");
  $.pnotify({
      title: 'Error',
      text: text,
      type: 'error'
  });
}

Note: The other answers won't work if you have a boolean value that you want to default to true. That's because anything||true will always return true. Instead you should do something like:

function foo(bar){
  if(bar === undefined)
    bar = true;
  //rest of your function
}

function [a-zA-Z0-9_]*?\s{0,}\([^=$)]*?(=)[^)$]*?\) it can be helpful to find js functions with default parameter and then correct them.


Need Your Help

NSData-AES Class Encryption/Decryption in Cocoa

cocoa nsdata encryption

I am attempting to encrypt/decrypt a plain text file in my text editor. encrypting seems to work fine, but the decrypting does not work, the text comes up encrypted. I am certain i've decrypted the...

Best way to take screenshots of tests in Selenium 2?

c# selenium screenshot functional-testing webdriver

I need a way to take screenshots of my functional tests. Right now I'm using Selenium 2 with C# bindings. I pretty much want to take a screenshot at the end of the test to make sure the desired pag...