16

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.

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132

4 Answers4

32

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'
    });
}
Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Now it shows ``Uncaught SyntaxError: Unexpected token ;`` for the line ``text = typeof text == 'undefined' ? "Something went wrong!" || text;`` – Anshad Vattapoyil Oct 31 '13 at 05:50
  • 1
    @devo because it is supported in firefox read the doc at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/default_parameters – Arun P Johny Oct 31 '13 at 05:56
  • I Like the early answer and +1 for other explanations. – Anshad Vattapoyil Oct 31 '13 at 06:04
  • Thanks - this worked for me as well - problem resolved for me too. The ironic part was (although totally not related - but still fun as Maps are made by Google and Chrome is made by Google) that my error came in when I used a default parameter for latlong co-ordinates. Chrome messing up with Google products :P – Kobus Myburgh Jan 23 '15 at 10:23
6

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'
  });
}
Mike Edwards
  • 3,742
  • 17
  • 23
  • Yes. It's working. Can you explain why it's working in firefox? – Anshad Vattapoyil Oct 31 '13 at 05:52
  • I am very surprised to hear that. If it's working in Firefox than Firefox must be forgiving your invalid syntax somehow, or the problem is just being swallowed so you didn't notice it. Your original code is invalid Javascript syntax, so it _should_ throw an error everywhere, unless there is a bug in the JS implementation. – Mike Edwards Oct 31 '13 at 05:54
  • 2
    @devo Default parameter values should become part of the language when [ECMAScript 6](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts) is finalized. Mozilla just added support for it early. – Jonathan Lonowski Oct 31 '13 at 05:55
  • AH. That's pretty cool, but unfortunately we really won't be able to write code that relies on that for a few years... – Mike Edwards Oct 31 '13 at 05:55
  • Thanks +1 for proper explanation. – Anshad Vattapoyil Oct 31 '13 at 06:05
1

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
}
Shelvacu
  • 4,245
  • 25
  • 44
0

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

123qwe
  • 1,525
  • 1
  • 14
  • 16