can someone explain to me why you can use parathesis instead os curly braces with an arrow function? I was thought that the parathesis are used to indicate to the compiler that what is encapsulated in the parathesis are supposed to be treated as parameters, and what is in the curly braces are supposed to be treated as the logic.
This goes to the point trying to be made by Fabio Russo "the good parts or right ways to code...It's not really the best way."
Your information is not incorrect for regular functions. However, with an arrow function, you are allowed to use parentheses to represent a function body with a single statement (useful when spanning multiple lines).
There are other places you can omit things as well.
Examples:
Single parameter, parentheses are optional
constmyFunc=(singleParam)=>{/* function body */};// is the same asconstmyFunc=singleParam=>{/* function body */};
Multiple parameters require parentheses
constmyFunc=(param1,param2)=>{/* function body */};constmyFunc=param1,param2=>{/* function body */};//Syntax error due to missing parentheses
Function body needs curly braces for multi-line command block
constmyFunc=param1=>{console.log('I did something');console.log('I did something else');};
However, if your body is a single line and you want to return the result, you may do any of the following (all of these return the value of name.toUpperCase()
constmyFunc=name=>{returnname.toUpperCase();// note the return};constmyFunc=name=>(name.toUpperCase();// note, no return if in parentheses);constmyFunc=name=>name.toUpperCase();
It really helps if you have a command that spans multiple lines where you just want to return the result. So, for example, if you were dealing with a promise you could do either of the following:
Now, I understand that you're able to use arrow functions in more diverse ways, but after looking at the code I had trouble with I realize that pranay rauthu used a "hidden if-statement" how does one use the ampersand(&) to make a conditional, and to see if arg === some-value, and print a value based on what the client set arg equal to.
If someone has a response to my question, please point me to some resource so I can deepen my knowledge on javascript
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think above code needs one more condition.
If I want to replace above code. I would do something like this. (with conditions)
LOL...nice.
can someone explain to me why you can use parathesis instead os curly braces with an arrow function? I was thought that the parathesis are used to indicate to the compiler that what is encapsulated in the parathesis are supposed to be treated as parameters, and what is in the curly braces are supposed to be treated as the logic.
This goes to the point trying to be made by Fabio Russo "the good parts or right ways to code...It's not really the best way."
Your information is not incorrect for regular functions. However, with an arrow function, you are allowed to use parentheses to represent a function body with a single statement (useful when spanning multiple lines).
There are other places you can omit things as well.
Examples:
Single parameter, parentheses are optional
Multiple parameters require parentheses
Function body needs curly braces for multi-line command block
However, if your body is a single line and you want to return the result, you may do any of the following (all of these return the value of name.toUpperCase()
It really helps if you have a command that spans multiple lines where you just want to return the result. So, for example, if you were dealing with a promise you could do either of the following:
Now, I understand that you're able to use arrow functions in more diverse ways, but after looking at the code I had trouble with I realize that pranay rauthu used a "hidden if-statement" how does one use the ampersand(&) to make a conditional, and to see if arg === some-value, and print a value based on what the client set arg equal to.
If someone has a response to my question, please point me to some resource so I can deepen my knowledge on javascript