0

Is it possible to not define function parameters and use a default value in that case?

For example.

function nutrition_facts(calories = 0, fat = 0, carb = 0, protein = 0) {
  cal += calories;
  f += fat;
  carbohydrate += carb;
  p += protein;
}

// Usage.
nutrition(calories = 10, protein = 10);

calories and protein would be passed in as 10, and fat and carb would be passed in as 0.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Roger
  • 83
  • 3
  • 9
  • why can't you refer this.. http://stackoverflow.com/a/12797135/3164682 – Sarath Jan 11 '17 at 12:31
  • Because I don't know what ES6 is and if that applies to me. – Roger Jan 11 '17 at 12:34
  • @Roger ECMAScript 5/6 is the latest JavaScript standard. Most browsers support it now. It introduces [true class OOP structures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) and allows you to use lambda functions (`=>`). ES6 introduced the ability to have default parameters. See the link in the previous comment. Here's an [ES6 browser compatibility table](https://kangax.github.io/compat-table/es6/). – Mr. Polywhirl Jan 11 '17 at 12:40
  • Here are the various browser implementations and which version they are using: https://en.wikipedia.org/wiki/ECMAScript#Implementations As you can see, most browsers are running a stable ES5 implementation. Chrome and Firefox both use ES6 as their baseline. – Mr. Polywhirl Jan 11 '17 at 12:47

1 Answers1

0

it IS possible when using ES2015(ES6) or TypeScript. In your example seems like you use ES5, so it's not possible

Romko
  • 1,808
  • 21
  • 27