Skip to main content

Arrays

QuickJS provides functions to create and work with JavaScript arrays.

Creating Arrays

JS_NewArray

Create a new empty JavaScript array.

Parameters

  • ctx - The JavaScript context

Returns

Returns a new empty array.

Example

JS_NewArrayFrom

Create a new array from a C array of values.

Parameters

  • ctx - The JavaScript context
  • count - Number of elements
  • values - Array of values (ownership transferred)

Returns

Returns a new array containing the specified values.

Example

Array Type Checking

JS_IsArray

Check if a value is an array.

Example

Note: This function no longer “punches through” proxies. To check if a proxy’s target is an array, use JS_GetProxyTarget() first.

Array Operations

Getting Array Length

Example

Setting Array Length

Example

Accessing Array Elements

Use property access functions with numeric indices:

Example

Iterating Over Arrays

Typed Arrays and Array Buffers

QuickJS also supports TypedArrays and ArrayBuffers through intrinsic functions. These are available after calling JS_AddIntrinsicTypedArrays():
  • JS_NewUint8Array() - Create Uint8Array
  • JS_NewArrayBuffer() - Create ArrayBuffer
  • JS_GetUint8Array() - Get data from Uint8Array
  • JS_GetArrayBuffer() - Get data from ArrayBuffer

Array-like Operations

Pushing Values

Calling Array Methods

Use JS_Invoke() to call array methods:

Notes

  • Arrays are regular objects with a length property
  • Use JS_SetPropertyUint32() for numeric indices
  • JS_NewArrayFrom() takes ownership of the values array
  • Array indices are uint32_t (0 to 2^32-1)
  • For large indices or negative indices, use JS_GetPropertyInt64()
  • Always check return values and free JSValues properly