Skip to main content

Value Creation

These functions create new JavaScript values from C types. All creation functions return a new JSValue that must be freed with JS_FreeValue() when no longer needed.

Primitive Values

JS_NewBool

Creates a JavaScript boolean value.
Parameters:
  • ctx - JavaScript context
  • val - Boolean value (0 for false, non-zero for true)
Returns: A new JavaScript boolean value Example:

JS_NewInt32

Creates a JavaScript number from a 32-bit integer.
Parameters:
  • ctx - JavaScript context
  • val - 32-bit signed integer value
Returns: A new JavaScript number Example:

JS_NewInt64

Creates a JavaScript number from a 64-bit integer. Uses int32 representation if the value fits, otherwise converts to float64.
Parameters:
  • ctx - JavaScript context
  • val - 64-bit signed integer value
Returns: A new JavaScript number

JS_NewUint32

Creates a JavaScript number from an unsigned 32-bit integer.
Parameters:
  • ctx - JavaScript context
  • val - 32-bit unsigned integer value
Returns: A new JavaScript number

JS_NewFloat64

Creates a JavaScript number from a double-precision floating point value.
Parameters:
  • ctx - JavaScript context
  • val - Double-precision floating point value
Returns: A new JavaScript number Example:

JS_NewNumber

Creates a JavaScript number from a double, choosing the optimal internal representation.
Parameters:
  • ctx - JavaScript context
  • d - Double value
Returns: A new JavaScript number

BigInt Values

JS_NewBigInt64

Creates a JavaScript BigInt from a 64-bit signed integer.
Parameters:
  • ctx - JavaScript context
  • v - 64-bit signed integer value
Returns: A new JavaScript BigInt

JS_NewBigUint64

Creates a JavaScript BigInt from a 64-bit unsigned integer.
Parameters:
  • ctx - JavaScript context
  • v - 64-bit unsigned integer value
Returns: A new JavaScript BigInt

String Values

JS_NewString

Creates a JavaScript string from a null-terminated C string.
Parameters:
  • ctx - JavaScript context
  • str - Null-terminated UTF-8 or ASCII string
Returns: A new JavaScript string Example:

JS_NewStringLen

Creates a JavaScript string from a C string with explicit length.
Parameters:
  • ctx - JavaScript context
  • str1 - UTF-8 or ASCII string (need not be null-terminated)
  • len1 - Length of the string in bytes
Returns: A new JavaScript string

JS_NewStringUTF16

Creates a JavaScript string from a UTF-16 encoded buffer.
Parameters:
  • ctx - JavaScript context
  • buf - UTF-16 encoded string buffer
  • len - Length in uint16_t units (not code points)
Returns: A new JavaScript string Note: The function makes a copy of the input buffer. It does not validate if the input is valid UTF-16 - that is the caller’s responsibility.

JS_NewAtomString

Creates a JavaScript string from a null-terminated C string, using atom optimization.
Parameters:
  • ctx - JavaScript context
  • str - Null-terminated UTF-8 or ASCII string
Returns: A new JavaScript string

Object Values

JS_NewObject

Creates a new JavaScript object with the default Object prototype.
Parameters:
  • ctx - JavaScript context
Returns: A new JavaScript object Example:

JS_NewObjectProto

Creates a new JavaScript object with a specific prototype.
Parameters:
  • ctx - JavaScript context
  • proto - Prototype object or JS_NULL
Returns: A new JavaScript object

JS_NewObjectClass

Creates a new JavaScript object of a specific class.
Parameters:
  • ctx - JavaScript context
  • class_id - Class ID for the object
Returns: A new JavaScript object of the specified class

JS_NewObjectProtoClass

Creates a new JavaScript object with a specific prototype and class.
Parameters:
  • ctx - JavaScript context
  • proto - Prototype object or JS_NULL
  • class_id - Class ID for the object
Returns: A new JavaScript object Example:

JS_NewObjectFrom

Creates a new JavaScript object from arrays of atoms and values.
Parameters:
  • ctx - JavaScript context
  • count - Number of properties
  • props - Array of property name atoms
  • values - Array of property values (ownership transferred)
Returns: A new JavaScript object Note: This function takes ownership of the values in the values array.

JS_NewObjectFromStr

Creates a new JavaScript object from arrays of string names and values.
Parameters:
  • ctx - JavaScript context
  • count - Number of properties
  • props - Array of property name strings
  • values - Array of property values (ownership transferred)
Returns: A new JavaScript object Note: This function takes ownership of the values in the values array.

Array Values

JS_NewArray

Creates a new JavaScript array.
Parameters:
  • ctx - JavaScript context
Returns: A new empty JavaScript array Example:

JS_NewArrayFrom

Creates a new JavaScript array from a C array of values.
Parameters:
  • ctx - JavaScript context
  • count - Number of elements
  • values - Array of values (ownership transferred)
Returns: A new JavaScript array Note: This function takes ownership of the values in the values array.

Special Values

QuickJS provides several predefined constant values:
Example:

Memory Management

All created values (except special constants) must be freed when no longer needed:
For functions taking ownership of values, you don’t need to free them: