The termSyntactic Sugar was coined byPeter J. Landin in 1964. In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.
The Why
It demonstrates the domain of native APIs, brings more flexibility and readability to the code, it can help others to find new ways to write more short and concise code, and I can go ahead all-day long.
When
Every time you have a change!
How... Let's go
// don'tletalex=personal.alex// do destructuringlet{alex}=personal
// don't if variable asignation may not varyvarisSuperHuman=superhuman?"is super human":"nahh"// use const insteadconstisSuperHuman=superhuman?"is super human":"nahh"
// arrays to avoid for loops o foreacharr.find(i=>i.id==="01")// returns the object// with destructuringarr.find(({id})=>id==="01")// returns the objectarr.filter(({id})=>id==="01")// returns and array of matches elementsarr.some(({id})=>id==="01")// returns boolean valuearr.every(({type})=>type==="superhumen")// returns boolean value if every nodes matched the criteriaarr.reduce((acc,{age})=>acc+age,0)// returns a reduced value in this case the sum of all ages// concat and push are from the pass now we spread!consta=[...arr,newElemet]// or to place the new element at the top of the arrayconsta=[newElemet,...arr]// mergin to arraysconsta=[...arrB,...arrC]// or flatted!!consta=[arrB,arrC].flat()// get the unique values (names) from array, lets map?consta=...newSet(data.map(i=>i.name))// as array?consta=[...newSet(data.map(i=>i.name))]
- But Alex I already know that man!
// number to strings?100.toString()// "100"// what about!100+""// "100"// strings to number?Number("100")// 100parseInt("100")// 100parseFloat("100").toFixed(2)// 100.00// what about!"100"*1// 100
- Of curse, I know that too!
If that is the case, my friend you may be the sweetest person in the whole world!
Just let me know how do you declare functions?
// as a declarationfunctionsum(a,b){returna+b}// as expressionconstsum=function(a,b){returna+b;};// as an arrow?constsum=(b,c)=>b+c// as shorthand method definition?constoperation={add:(a,b)=>a+b}// orconstoperation={add(a,b){returna+b}
See ya at the next sugar! Let me know yours.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse