# math.nearestPowerOfTwo

Function · category: Math

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/core/math/math.js#L227

```ts
nearestPowerOfTwo(val: number): number
```

Returns the nearest (smaller or larger) power of 2 for the specified value.

**Parameters**

- `val` (`number`): The value for which to calculate the nearest power of 2.

**Returns** `number`: The nearest power of 2.

**Example**

```ts
math.nearestPowerOfTwo(17); // returns 16
math.nearestPowerOfTwo(24); // returns 32
```
