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.
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