We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

JS NewString

This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Note: JS_NewString() was removed in SpiderMonkey 1.8.5. Use JS_NewUCString() or JS_NewStringCopyN() instead.


JSString * JS_NewString(JSContext *cx, char *buf, size_t length);

JSString * JS_NewUCString(JSContext *cx, jschar *buf, size_t length);
Name Type Description
cx JSContext * The context in which to create the new string. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
buf char * or jschar * Pointer to a character array, allocated with JS_malloc, containing the characters for the JS string to create. The JavaScript engine adopts the buffer.
length size_t Number of characters in the text string.


JS_NewString creates and returns a new string, using the memory starting at buf and ending at buf + length as the character storage. JS_NewUCString is the Unicode version of the function.

The character array, buf, must be allocated on the heap using JS_malloc. On success, the JavaScript engine adopts responsibility for memory management of this region. The application must not read, write, or free the buffer. This allows the JavaScript engine to avoid needless data copying.

On success, JS_NewString and JS_NewUCString return a pointer to the new string. On error or exception, they return NULL.

See Also

MXR ID Search for JS_NewString
MXR ID Search for JS_NewUCString

JS_ConvertValue, JS_GetEmptyStringValue, JS_GetStringBytes, JS_GetStringChars, JS_GetStringLength, JS_InternString, JS_InternUCString, JS_InternUCStringN, JS_NewStringCopyN, JS_NewStringCopyZ, JS_NewUCString, JS_NewUCStringCopyN, JS_NewUCStringCopyZ, JS_ValueToString

Document Tags and Contributors

Last updated by: Jorend,