What are your favorite programming language syntax features?
Tags: discuss, programming, beginners, software
Published: 2019-03-27 16:31:17

---
title: What are your favorite programming language syntax features?
published: true
description: Programming Language Syntax
tags: discuss, programming, beginners, software
---

I have my own non-exhaustive list, and these are purely based on what I have used: *JavaScript, TypeScript, C#*

#Safe Navigation Operator
This is also known as *optional chaining operator*, *safe call operator*, *null-conditional operator*.

Instead of writing multiple nested ifs, we just use usual chaining, but put question mark symbols `?` before dots (or other characters used for chaining).

```c#
String name = articles?[0]?.author?.name;
```

#Null Coalescing Operator
This is the shorthand of the ternary conditional if operator `x ? x : y`

```c#
string pageTitle = suppliedTitle ?? "Default Title";
```

In JavaScript the OR operator `||` has the same behavior as the above. It returns the first operand if it evaluates to true, otherwise, it returns the second operand. When the left hand side is true, the right hand side is not even evaluated; it is "short-circuited."

```javascript
 let pageTitle = suppliedTitle || "Default Title"
```

# Lambda
This is also known as anonymous function, function literal, lambda abstraction, or lambda expression.
 
```javascript
const hello = (name) => `Hello ${name}`
```

You can do a lot of things with this construct. [Currying](https://medium.com/javascript-scene/curry-and-function-composition-2c208d774983), [closures](https://dev.to/yaminmhd/understanding-javascript-closures-1fk7), [high order functions](https://eloquentjavascript.net/05_higher_order.html) etc. 

# Auto Implemented Properties

In C# auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. 

```csharp
public string Name { get; set; }
```
In C# 6 and later, you can initialize auto-implemented properties similarly to fields:

```csharp
public string FirstName { get; set; } = "Jane"; 
```

You can learn more about automatic properties [here](https://gunnarpeipman.com/csharp/automatic-properties/)

# Async/Await

The async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.

C#

```csharp
public async Task<int> FindPageSize(Uri uri) 
{
    byte[] data = await new WebClient().DownloadDataTaskAsync(uri);
    return data.Length;
}
```

JS
```js
async function createNewDoc() {
  let response = await db.post({}); // post a new doc
  return await db.get(response.id); // find by id
}
```
# Intersection / Union Types

In TypeScript, the intersection type combines types, so that you can have all properties and members of both types on your particular object or method.

```ts
target: string & Element;
```

Union types, meanwhile, are an either/or scenario. Instead of combining types to receive all members and properties, we use union types to specify that a method accepts either of several different types.

```ts
target: number | string;
```
# JSON Serialize / Deserialize

JavaScript makes it very easy for us to serialize and deserialize JSON.

```js
const obj = { name: 'Aivan' }
JSON.stringify(obj) // serialize object to JSON string

const str = `{name : "Aivan"}`
JSON.parse(str) // deserialize string to object 
```

# Template Literals

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

```js
`string text 
   ${expression} 
 string text`
```

# Destructuring and Spread Operator

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

```js
var a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20

[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20

({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
```
This is taken from the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)

# Object Property Value Shorthand

Instead of writing your objects like this:

```js
const name = 'Aivan'
const obj = { name: name } // { name: 'Aivan'}
```

You can write it like this. This is possible if the name of the key and the variable name of the value is the same.

```js
const name = 'Aivan'
const obj = { name } // { name: 'Aivan'}
```

# Annotations / Decorators

Decorators provide a very simple syntax for calling higher-order function. It also allows for a cleaner syntax for applying wrappers around your code, resulting in something that detracts less from the actual intention of what you’re writing.

```js
@Wove()
class DataMapper {
  // ...
}

```

You can learn more about decorators [here](https://blog.mgechev.com/2018/01/29/javascript-decorators-aop-autobind-memoization/).

# Default Parameters

Default parameters allow named parameters to be initialized with default values if no value or undefined is passed.

```js
function multiply(a, b = 1) {
  return a * b;
}

// you can call it like this
multiply(1) // 1
multiply(2, 2) // 4
```