The default value of the arrow function

I am new to ES6 Javascript, which means that I am learning it. I like the arrow function and the default parameter function from ES6, which is mentioned on the site below.

http://es6-features.org/#ExpressionBodies
http://es6-features.org/#DefaultParameterValues

Below is my ES6 code snippet, and I tested it in Chrome 47 . I am trying to give a default parameter value for my arrow function, which is currently throwing an error like

 <script type="text/javascript"> 'use strict'; var greet = (name = 'Venkat') => (console.log('Hi ' + name + '!')); greet(); // expected output: Venkat greet('Venkatraman'); // expected output: Venkatraman </script> 

Let me know if this is possible, if so, explain with the solution and what I'm doing wrong here.

+9
source share
2 answers

No, this is not possible (for now, I suppose). What can you do though:

 var greet = name => console.log('Hi ' + (name || 'Venkat') + '!'); greet(); // output: Venkat greet('Venkatraman'); // output: Venkatraman 

Try here

[ January 2018 ] The default setting is now supported in all major browsers.

+8
source

Source: https://habr.com/ru/post/1238627/


All Articles