Working with JavaScript values
Source: Node.js documentationNode-API exposes a set of APIs to create all types of JavaScript values. Some of these types are documented under Section language types of the ECMAScript Language Specification.
Fundamentally, these APIs are used to do one of the following:
- Create a new JavaScript object
- Convert from a primitive C type to a Node-API value
- Convert from Node-API value to a primitive C type
- Get global instances including
undefinedandnull
Node-API values are represented by the type napi_value.
Any Node-API call that requires a JavaScript value takes in a napi_value.
In some cases, the API does check the type of the napi_value up-front.
However, for better performance, it's better for the caller to make sure that
the napi_value in question is of the JavaScript type expected by the API.
Enum types
Object creation functions
Functions to convert from C types to Node-API
Functions to create optimized property keys
Many JavaScript engines including V8 use internalized strings as keys to set and get property values. They typically use a hash table to create and lookup such strings. While it adds some cost per key creation, it improves the performance after that by enabling comparison of string pointers instead of the whole strings.
If a new JavaScript string is intended to be used as a property key, then for
some JavaScript engines it will be more efficient to use the functions in this
section. Otherwise, use the napi_create_string_utf8 or
node_api_create_external_string_utf8 series functions as there may be
additional overhead in creating/storing strings with the property key
creation methods.
Functions to convert from Node-API to C types
Functions to get global instances
APIs in this section
Enum types
napi_key_collection_modev6Describes the Keys/Properties filter enums:napi_key_filterv6Property filter bit flag. This works with bit operators to build a composite filter.napi_key_conversionv6napi_key_numbers_to_strings will convert integer indexes to strings. napi_key_keep_numbers will return numbers for integer indexes.napi_valuetypeDescribes the type of a napi_value. This generally corresponds to the types described in Section language types of the ECMAScript Language Specification.napi_typedarray_typeThis represents the underlying binary scalar datatype of the TypedArray. Elements of this enum correspond to Section TypedArray objects of the ECMAScript Language Specification.
Object creation functions
napi_create_arrayv1This API returns a Node-API value corresponding to a JavaScript Array type. JavaScript arrays are described in Section Array objects of the ECMAScript Language Specification.napi_create_array_with_lengthv1This API returns a Node-API value corresponding to a JavaScript Array type. The Array's length property is set to the passed-in length parameter.napi_create_arraybufferv1This API returns a Node-API value corresponding to a JavaScript ArrayBuffer. ArrayBuffers are used to represent fixed-length binary data buffers. They are normally used as a backing-buffer for TypedArray objects.napi_create_bufferv1This API allocates a node::Buffer object. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice.napi_create_buffer_copyv1This API allocates a node::Buffer object and initializes it with data copied from the passed-in buffer. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice.napi_create_datev5This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.napi_create_externalv1This API allocates a JavaScript value with external data attached to it. This is used to pass external data through JavaScript code, so it can be retrieved later by native code using napi_get_value_external.napi_create_external_arraybufferv1In order to maintain broadest compatibility with all runtimes you may define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED in your addon before includes for the node-api headers.node_api_create_external_sharedarraybufferCreate a SharedArrayBuffer with externally managed memory.napi_create_external_bufferv1In order to maintain broadest compatibility with all runtimes you may define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED in your addon before includes for the node-api headers.napi_create_objectv1This API allocates a default JavaScript Object. It is the equivalent of doing new Object() in JavaScript.node_api_create_object_with_propertiesexperimentalThis API creates a JavaScript Object with the specified prototype and properties.napi_create_symbolv1This API creates a JavaScript symbol value from a UTF8-encoded C string.node_api_symbol_forv9This API searches in the global registry for an existing symbol with the given description. If the symbol already exists it will be returned, otherwise a new symbol will be created in the registry.napi_create_typedarrayv1This API creates a JavaScript TypedArray object over an existing ArrayBuffer or SharedArrayBuffer.node_api_create_buffer_from_arraybufferv10This API creates a JavaScript Buffer object from an existing ArrayBuffer. The Buffer object is a Node.js-specific class that provides a way to work with binary data directly in JavaScript.napi_create_dataviewv1This API creates a JavaScript DataView object over an existing ArrayBuffer or SharedArrayBuffer.
Functions to convert from C types to Node-API
napi_create_int32v1This API is used to convert from the C int32_t type to the JavaScript number type.napi_create_uint32v1This API is used to convert from the C uint32_t type to the JavaScript number type.napi_create_int64v1This API is used to convert from the C int64_t type to the JavaScript number type.napi_create_doublev1This API is used to convert from the C double type to the JavaScript number type.napi_create_bigint_int64v6This API converts the C int64_t type to the JavaScript BigInt type.napi_create_bigint_uint64v6This API converts the C uint64_t type to the JavaScript BigInt type.napi_create_bigint_wordsv6This API converts an array of unsigned 64-bit words into a single BigInt value.napi_create_string_latin1v1This API creates a JavaScript string value from an ISO-8859-1-encoded C string. The native string is copied.node_api_create_external_string_latin1v10This API creates a JavaScript string value from an ISO-8859-1-encoded C string. The native string may not be copied and must thus exist for the entire life cycle of the JavaScript value.napi_create_string_utf16v1This API creates a JavaScript string value from a UTF16-LE-encoded C string. The native string is copied.node_api_create_external_string_utf16v10This API creates a JavaScript string value from a UTF16-LE-encoded C string. The native string may not be copied and must thus exist for the entire life cycle of the JavaScript value.napi_create_string_utf8v1This API creates a JavaScript string value from a UTF8-encoded C string. The native string is copied.
Functions to create optimized property keys
node_api_create_property_key_latin1v10This API creates an optimized JavaScript string value from an ISO-8859-1-encoded C string to be used as a property key for objects. The native string is copied.node_api_create_property_key_utf16v10This API creates an optimized JavaScript string value from a UTF16-LE-encoded C string to be used as a property key for objects. The native string is copied.node_api_create_property_key_utf8v10This API creates an optimized JavaScript string value from a UTF8-encoded C string to be used as a property key for objects. The native string is copied.
Functions to convert from Node-API to C types
napi_get_array_lengthv1This API returns the length of an array.napi_get_arraybuffer_infov1This API is used to retrieve the underlying data buffer of an ArrayBuffer or SharedArrayBuffer and its length.napi_get_buffer_infov1This method returns the identical data and byte_length as napi_get_typedarray_info. And napi_get_typedarray_info accepts a node::Buffer (a Uint8Array) as the value too.napi_get_prototypev1napi_get_typedarray_infov1This API returns various properties of a typed array.napi_get_dataview_infov1Any of the out parameters may be NULL if that property is unneeded.napi_get_date_valuev5This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.napi_get_value_boolv1This API returns the C boolean primitive equivalent of the given JavaScript Boolean.napi_get_value_doublev1This API returns the C double primitive equivalent of the given JavaScript number.napi_get_value_bigint_int64v6This API returns the C int64_t primitive equivalent of the given JavaScript BigInt. If needed it will truncate the value, setting lossless to false.napi_get_value_bigint_uint64v6This API returns the C uint64_t primitive equivalent of the given JavaScript BigInt. If needed it will truncate the value, setting lossless to false.napi_get_value_bigint_wordsv6This API converts a single BigInt value into a sign bit, 64-bit little-endian array, and the number of elements in the array. sign_bit and words may be both set to NULL, in order to get only word_count.napi_get_value_externalv1This API retrieves the external data pointer that was previously passed to napi_create_external().napi_get_value_int32v1This API returns the C int32 primitive equivalent of the given JavaScript number.napi_get_value_int64v1This API returns the C int64 primitive equivalent of the given JavaScript number.napi_get_value_string_latin1v1This API returns the ISO-8859-1-encoded string corresponding the value passed in.napi_get_value_string_utf8v1This API returns the UTF8-encoded string corresponding the value passed in.napi_get_value_string_utf16v1This API returns the UTF16-encoded string corresponding the value passed in.napi_get_value_uint32v1This API returns the C primitive equivalent of the given napi_value as a uint32_t.