Search code examples
javascriptecmascript-6default-parameterskhan-academy

Khanacademy Javascript Canvas default parameter error


I'm working on a cool program on the khanacademy.org javascript/ProcessingJS canvas, and I want to use default parameters. However, it showed this error box:

'default parameters' is only available in ES6 (use esnext option).

What is that and how do I fix it?


Solution

  • Using default parameters in this way will give the warning you mentioned:

    var foo = function(param1, param2 = "some default value"){
      console.log(param1 + " " + param2);
    }
    

    Assigning default parameters in the way described above is an ECMAScript 6 feature and is currently only supported by Mozilla Firefox. Check browser compatibility here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters#Browser_compatibility

    Generally default parameters are assigned in javascript in the following way. I suggest you to follow the same, they will work fine in all browsers:

    var foo = function(param1, param2){
       param2 = typeof param2 !== 'undefined' ?  param2 : "some default value";
       console.log(param1 + " " + param2);
    }
    

    Check this link for more details: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters