Does const work as function in ES6 sometimes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does const work as function in ES6 sometimes?

Like in this code, const a="hello"; console.log(a); here it works just like a variable. But in here, const add(x,y)=>{ let sum=x+y; console.log (sum); } But here it treats like a function. Why is that? Help me to understand this.

18th May 2020, 8:19 AM
Zulkar Prince
Zulkar Prince - avatar
2 Answers
+ 1
const can not be used to define functions. Your second example will report a syntax error. However you can assign a function to a constant variable like this: const add = (x,y) => { let sum = x + y; console.log(sum); }
18th May 2020, 9:19 AM
Ore
Ore - avatar
0
because you’re assigning the value of the constant variable `add` to a function. In javascript, a variables can have a function as a value. the way you are defining a function is with the lambda shorthand way introduced in ES6.
18th May 2020, 8:25 AM
Brian R
Brian R - avatar