qjs executable, several global functions and variables are automatically available without requiring module imports.
These globals are added by the
qjs executable and are not available when embedding QuickJS in other applications unless explicitly configured.Global Variables
argv0
The full path to the executable that is running the script.
scriptArgs
Command line arguments passed to the script. The first element is always the script filename.
navigator.userAgent
Returns a string in the format
quickjs-ng/<version>.Global Functions
print(...args)
Prints the arguments separated by spaces with a trailing newline.
Any number of arguments to print. Non-string values are converted to strings.
console.log(...args)
Alias for print(). Behaves identically to print().
Any number of arguments to print.
Unlike browser or Node.js environments,
console.log in QuickJS-ng is simply an alias for print() and does not support advanced formatting or object inspection.gc()
Manually triggers garbage collection.
This function does not return a value.
When should I use gc()?
When should I use gc()?
The garbage collector runs automatically when needed. Manual invocation is useful in specific scenarios:
- Memory-constrained environments - Force cleanup before starting memory-intensive operations
- Testing and benchmarking - Ensure consistent memory state between test runs
- Long-running scripts - Periodically clean up in scripts that run for extended periods
gc() is a shorthand for std.gc() from the qjs:std module.Comparison with Other Environments
Node.js Compatibility
Node.js Compatibility
QuickJS-ng globals differ from Node.js:
| QuickJS-ng | Node.js | Notes |
|---|---|---|
argv0 | process.argv[0] | QuickJS-ng includes only the executable path |
scriptArgs | process.argv.slice(1) | QuickJS-ng includes the script name |
print() | console.log() | QuickJS-ng print() is simpler |
gc() | global.gc() | Node.js requires --expose-gc flag |
Browser Compatibility
Browser Compatibility
QuickJS-ng globals differ from browsers:
| QuickJS-ng | Browser | Notes |
|---|---|---|
argv0 | Not available | Browser concept doesn’t apply |
scriptArgs | Not available | Use URL parameters in browsers |
print() | window.print() | Different function entirely |
navigator.userAgent | navigator.userAgent | Similar concept, different format |
Example: Command-Line Script
Next Steps
std Module
Explore the standard library module
os Module
Learn about OS-level functions
Overview
Return to stdlib overview