Syntactic sugar

Layman (level 1)

A programming language feature designed to make code easier for humans to read and write.

Beginner (level 2)

Syntax element within a programming language designed to make things easier to express and read. Does not add new functionality to the language, but rather provides a simpler way to express otherwise complex or cumbersome code.

Expert (level 3)

Special notations for special applications added to a programming language in order to make life easier for programmers. Does not add expressive power to the language. Can always be rewritten in a more analytic way.


Examples

  • In many C-like languages x+=y is sugar for x = x + y.
  • In Haskell, do x <- f; g x is sugar for f >>= (\x -> g x).
  • In Python, list slicing a[:4] is sugar for a[0:4].
  • JSX can be considered syntactic sugar, because the whole markup notation is essentially a way to generate JS code:

    <Button onClick={() => alert('YES')}>Click me</Button>
    

    is translated to:

    React.createElement(Button, { onClick: () => alert('YES') }, 'Click me');
    

Read

Watch