I'm currently studying basic javascript features, and I am quite struggling with the arrow function method. So I from what I've learnt, if there is only one parameter, it doesn't matter if we use parenthesis or not like the code below:
(singleParam) => { statements }
singleParam => { statements }
Then when emitting events in socket IO, to simplify the codes
// server-side
io.on("connection", (socket) => {
socket.emit("hello", "world");
});
// client-side
socket.on("hello", (arg) => {
console.log(arg); // world
});
could be simplified as
// server-side
io.on("connection", socket => {
socket.emit("hello", "world");
});
// client-side
socket.on("hello", arg => {
console.log(arg); // world
});
This is working fine in my project, thus I wonder the necessity of using parenthesis when using arrow method function. Maybe I'm not fully grasping the concepts, can anyone explain this method a little bit easier way? Thanks.
If using exactly one parameter in an arrow function you don't need to use the parenthesis. Some developers prefer to always add them however for consistency and readability.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions