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.
2 Respostas
+ 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);
}
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.