This is an archived page. It's not actively maintained.


Converts any JavaScript value to an object.


JS_ValueToObject(JSContext *cx, JS::HandleValue v, JS::MutableHandleObject objp);
Name Type Description
cx JSContext * The context in which to convert the value. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
v JS::HandleValue The value to convert.
objp JS::MutableHandleObject Out parameter. On success, *objp receives NULL or a pointer to the resulting object.


JS_ValueToObject converts a specified JavaScript value, v, to an object. On success, this function stores either NULL or a pointer to the resulting object in *objp and returns true. Otherwise it returns false and the value left in *objp is unspecified.

  • If v is JSVAL_NULL or JSVAL_VOID, the result is NULL.
  • If v is a boolean value, a number, or a string, the result is a new wrapper object of type Boolean, Number, or String.
  • Otherwise v is an object, and the result depends on the object. If v is a native JavaScript Object, this calls the object's valueOf method, if any. In any case, the result is not guaranteed to be the same object as v. (Implementation note: the object's JSObjectOps.defaultValue method is called with hint=JSTYPE_OBJECT.)

The resulting object is subject to garbage collection unless the variable *objp is protected by a local root scope, an object property, or the JS_AddRoot function. Note that a local root scope is not sufficient to protect the resulting object in some cases involving the valueOf method!

See Also