Create a new Array object.


JSObject * JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
Name Type Description
cx JSContext * The context in which to create the new array. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
length jsint The length of the new array. This must not be negative.
vector jsval * Pointer to the initial values for the array's elements, or NULL.


JS_NewArrayObject creates a new array object with the specified length. If vector is non-null, then for each index i from 0 to length - 1, JS_NewArrayObject defines an enumerable array element with the value vector[i] on the new array. (This means that if length is nonzero and vector is null, the result is like the JavaScript expression new Array(length). The new array has the specified length, but it doesn't have any elements.)

On success, JS_NewArrayObject returns the new array object. Otherwise it reports an error as though by calling JS_ReportOutOfMemory and returns NULL.

It is often better to call JS_NewArrayObject(cx, 0, NULL), store the returned object in a GC root, and then populate its elements with JS_SetElement or JS_DefineElement. This avoids unrooted jsvals in vector from being subject to garbage collection until the new object has been populated.

MXR ID Search for JS_NewArrayObject

Document Tags and Contributors

Contributors to this page: MMondor, Dria, Jorend
Last updated by: Jorend,