Skip to main content
When running scripts with the 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

argv0
string
The full path to the executable that is running the script.
console.log('Executable:', argv0);
// Output: Executable: /usr/local/bin/qjs

scriptArgs

scriptArgs
string[]
Command line arguments passed to the script. The first element is always the script filename.
// Running: qjs script.js arg1 arg2
console.log(scriptArgs);
// Output: ['script.js', 'arg1', 'arg2']
navigator.userAgent
string
Returns a string in the format quickjs-ng/<version>.
console.log(navigator.userAgent);
// Output: quickjs-ng/0.8.0

Global Functions

Prints the arguments separated by spaces with a trailing newline.
...args
any
Any number of arguments to print. Non-string values are converted to strings.
print('Hello', 'World', 123);
// Output: Hello World 123

console.log(...args)

Alias for print(). Behaves identically to print().
...args
any
Any number of arguments to print.
console.log('Status:', 'OK');
// Output: Status: OK
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.
return
undefined
This function does not return a value.
// Create many temporary objects
for (let i = 0; i < 1000000; i++) {
  const obj = { value: i };
}

// Force garbage collection
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
In most cases, you should rely on automatic garbage collection.
gc() is a shorthand for std.gc() from the qjs:std module.

Comparison with Other Environments

QuickJS-ng globals differ from Node.js:
QuickJS-ngNode.jsNotes
argv0process.argv[0]QuickJS-ng includes only the executable path
scriptArgsprocess.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
QuickJS-ng globals differ from browsers:
QuickJS-ngBrowserNotes
argv0Not availableBrowser concept doesn’t apply
scriptArgsNot availableUse URL parameters in browsers
print()window.print()Different function entirely
navigator.userAgentnavigator.userAgentSimilar concept, different format

Example: Command-Line Script

#!/usr/bin/env qjs

if (scriptArgs.length < 2) {
  print('Usage:', scriptArgs[0], '<filename>');
  std.exit(1);
}

const filename = scriptArgs[1];
print('Processing file:', filename);

// Process file...
const content = std.loadFile(filename);
if (content === null) {
  print('Error: Could not read file');
  std.exit(1);
}

print('File size:', content.length, 'bytes');
print('User agent:', navigator.userAgent);

Next Steps

std Module

Explore the standard library module

os Module

Learn about OS-level functions

Overview

Return to stdlib overview

Build docs developers (and LLMs) love