# Future JavaScript

## Top-level `await`
```js
// index.js

await myFunction() // no need to run it with async on top level.

``` 

## Object.hasOwn()

```js
const myObject = {
  name: 'dav1',
  age: 28
}

Object.hasOwn(myObject, 'name') // true
```
## Error cause
```
const parentError = new Error('Parent error')

const childError = new Error('Child error', { cause: parentError })
```
## Array.at()
```js
[1,2,3,4,5,6].at(-2) // 5
```
## Set

```js
const arrayWithDuplicates = [1,2,3,4,4,4,5,6,7]
const mySet = New Set(arrayWithDuplicates) // [1,2,3,4,5,6,7]
const arrayWithoutDuplicates = [...mySet]
```
## Temporal
```js
const myDate = Temporal.now.plainDateISO()
console.log(myDate) // 2021-09-03
console.log(myDate.add({ days: 2 })) //2021-09-05
```

## Array.findLast() and Array.findLastIndex()
```js
const userData = [{
    name: 'John', 
    age: 23
  },{
    name: 'Maria',
    age: 34
  },{
    name: 'John',
    age: 33
  }
]

userData.findLast(user => user.name === 'John') // { name: 'John', age: 33 }
userData.findLastIndex(user => user.name === 'John') // 2
```
## Throw Expressions
```js
function addDays( date = throw new TypeError("Argument required")) {
   //...
}
```

## Import()
```js
const modules = await import("module-name");
```
## Pipeline 
```js
const a = 4
a |> multiplyBy2(^) |> addFour(^) |> addOne(^) // 13
```

## Logical Nullish Assignment
```js
let a = 10
a ??= 14
console.log(a) // 10
let b = undefined
b ??= 14
console.log(b) // 14
```

## Numeric separators
```js
const a = 1_000_000 // make it easy to read and understand
```

## String.replaceAll()
```js
let a = 'money money money car money'.replaceAll('money', 'bees')
// bees bees bees car bees
```

## Records and Tuples
```js
const a = #{
  b: 'This cannot be changed!'
}

const b = #[1,2,3,'This cannot be changed either']
```
## Optional catch binding
```js
try {
  doSomething();
} catch { //No need to bind the errror!
  handleException(); 
}
```

## Module Blocks
```js
// this can be useful when dealing with workers.
const moduleBlock = module {
	export async function main(url) {
		return import.meta.url;
	}
}
```
