Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Alex Suarez
Alex Suarez

Posted on • Edited on

     

Syntactic sugar, why, when and how

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
Enter fullscreen modeExit fullscreen mode
// don't if variable asignation may not varyvarisSuperHuman=superhuman?"is super human":"nahh"// use const insteadconstisSuperHuman=superhuman?"is super human":"nahh"
Enter fullscreen modeExit fullscreen mode
// 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))]
Enter fullscreen modeExit fullscreen mode
  • 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
Enter fullscreen modeExit fullscreen mode
  • 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}
Enter fullscreen modeExit fullscreen mode

See ya at the next sugar! Let me know yours.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Frontend Engineer
  • Location
    Amsterdam, Netherlands
  • Work
    Senior Frontend Engineer
  • Joined

More fromAlex Suarez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp