2

So I was fiddling around with function overloading (I believe thats the correct term for this). Heres what happened:

function example(a=3,b=6){
   console.log(a);
   console.log(b);
}

In firefox, this did exactly what I expected.

example()
3
6
example(17)
17
6
example(10,20)
10
20

However, when I tried this in the console in Chrome, it failed to even create the function. I got error

SyntaxError: Unexpected token =

Why is this happening?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
MikeTGW
  • 395
  • 2
  • 10
  • possible duplicate of [Set a default parameter value for a JavaScript function](http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function) – Ram Mar 16 '14 at 07:06
  • @undefined: The question you linked to as duplicate does not mention ES6 default parameters at all. – slebetman Mar 16 '14 at 07:14
  • 1
    I wasn't asking if you could do it. I am more curious as to why it works in one up-to-date browser, and not in another. – MikeTGW Mar 16 '14 at 08:43
  • @undefined: As a suggestion, try to understand the question before flagging it. – slebetman Mar 16 '14 at 23:28
  • @undefined: As a suggestion, try not to read between the lines and read the question per se. The question was: why was the behavior different between browsers, not how default values can be set. – slebetman Mar 17 '14 at 02:55
  • @undefined: Do YOU read the comments. Read the one the OP himself posted. – slebetman Mar 17 '14 at 03:14

1 Answers1

3

Default values to functions are part of ECMA Script 6 specifications. You might be using the latest version of FireFox in which they would have implemented it.

All the browsers which havn't implemented the ES6 specifications won't be able to parse the expression. That is why it is failing.

You can check Kangax's compatibility table to know where your browser supports it or not.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/default_parameters – chiliNUT Mar 16 '14 at 07:06
  • @chiliNUT Correct. If you look at the specification section of that page, as I said, this feature is part of ES6 and FF has implemented it. – thefourtheye Mar 16 '14 at 07:08
  • Wow thank you. That looks like a very useful website. Im still a little new to javascript, and that site has never turned up in my google searching. – MikeTGW Mar 16 '14 at 08:42