Skip to main content

TextEncoder

Class

Encodes strings into UTF-8 byte arrays.

Index

Example

// Encode a string to UTF-8 bytes with TextEncoder
const encoder = new TextEncoder();
const encoded = encoder.encode('Hello World');
console.log('Encoded bytes length:', encoded.length);
console.log('Encoder encoding:', encoder.encoding);

// Decode bytes back to string with TextDecoder
const decoder = new TextDecoder();
const decoded = decoder.decode(encoded);
console.log('Decoded string:', decoded);
console.log('Decoder encoding:', decoder.encoding);

// Verify round-trip: encode then decode produces the original string
const original = 'Lens Studio Editor API';
const roundTrip = new TextDecoder().decode(new TextEncoder().encode(original));
console.log('Round-trip match:', original === roundTrip);

// encodeInto writes directly into an existing Uint8Array
const buffer = new Uint8Array(32);
encoder.encodeInto('Test', buffer);
console.log('encodeInto first byte (T = 84):', buffer[0]);

Extends

Constructors

Constructor

new TextEncoder(): TextEncoder

Creates a new TextEncoder instance.

Returns

TextEncoder

Overrides

ScriptObject.constructor

Properties

encoding

readonly encoding: string

The encoding format used by this encoder, always 'utf-8'.

Methods

encode()

encode(value): Uint8Array

Encodes a string into a Uint8Array using UTF-8.

Parameters

ParameterType
valuestring

Returns

Uint8Array


encodeInto()

encodeInto(value, result): void

Encodes a string into an existing Uint8Array in place.

Parameters

ParameterType
valuestring
resultUint8Array

Returns

void


getTypeName()

static getTypeName(): string

Returns the type name of this class.

Returns

string

Overrides

ScriptObject.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

ScriptObject.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object is of the specified type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

ScriptObject.isOfType


isSame()

isSame(other): boolean

Returns true if this object refers to the same instance as the given object.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

ScriptObject.isSame