---
title: TextEncoder
summary: An implementation of the WHATWG Encoding Standard TextEncoder API. All instances of TextEncoder only support UTF-8 encoding.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/class.TextEncoder/
---

**Kind:** Class  
**Module:** [globals](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/)  
**Package:** @fastly/js-compute 4.0.1  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L917

An implementation of the [WHATWG Encoding Standard](https://encoding.spec.whatwg.org/) `TextEncoder` API. All
instances of `TextEncoder` only support UTF-8 encoding.

TextEncoder takes a stream of code points as input and emits a stream of UTF-8 bytes.

**See also**

- [TextEncoder on MDN](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)

```js
const encoder = new TextEncoder();
const uint8array = encoder.encode('a string to encode');
```

## Constructor

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L921

```javascript
new TextEncoder()
```

Returns a newly constructed TextEncoder that will generate a byte stream with UTF-8 encoding.

## Properties

### encoding

**Type:** `'utf-8'`  
**Flags:** read-only  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L926

The TextEncoder.encoding read-only property returns a string containing the name of the encoding algorithm used by the specific encoder.
It is always set to the value "utf-8".

## Methods

### encode

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/globals.d.ts#L931

```javascript
encode(input?: string): Uint8Array<ArrayBuffer>
```

UTF-8 encodes the `input` string and returns a `Uint8Array` containing the encoded bytes.

**Parameters**

- `input`: `string` (optional)

**Returns**

`Uint8Array<ArrayBuffer>`
