0
Anonymous function in javascript
What is anonymous function in javascript?explain with an example.
4 Respuestas
+ 2
It's a function that was declared but not assigned to any name indentifier, thus, it cannot be used again after its initial declaration.
A non-anonymous function would be written as:
___
function hello(){ return 'hello' }
___
or
___
const hello = () = { return 'hello' }
___
A anonymous function would be written as:
___
function() { return 'anonymous' }
___
or
___
() = { return 'anonymous'}
___
It has many uses but 90% of the time you'll use it in Higher Order Functions as an argument, for example, doubling all numbers of an array with the map method:
___
const nums = [1, 2, 3]
//Double all numbers with an anonymous arrow function
const doubles = nums.map( num => { return num * 2} )
//Output [2, 4, 9]
console.log(doubles)
___
+ 2
wow!thanks!👍👍👍
0
Hey, just noticed a mistake. The output should be [2, 4, 6], lol. Anonymous functions are also common as callbacks. Sorry!
0
lol..its ok 😂😅