title: ‘Using d8

description: ‘d8 is V8’s own developer shell.’

d8 is V8’s own developer shell.

d8 is useful for running some JavaScript locally or debugging changes you have made to V8. Building V8 using GN for x64 outputs a d8 binary in out.gn/x64.optdebug/d8. You can call d8 with the --help argument for more information about usage and flags.

Print to the command line

Printing output is probably going to be very important if you plan to use d8 to run JavaScript files rather than interactively. This can be achieved using console.log:

  1. $ cat test.js
  2. console.log('Hello world!');
  3. $ out.gn/x64.optdebug/d8 test.js
  4. Hello world!

d8 also comes with a global print function that does the same thing. However, console.log is preferred over print since it works in web browsers as well.

Read input

Using read() you can store the contents of a file into a variable.

  1. d8> const license = read('LICENSE');
  2. d8> license
  3. "This license applies to all parts of V8 that are not externally
  4. maintained libraries. The externally maintained libraries used by V8
  5. are:
  6. (etc.)"

Use readline() to interactively enter text:

  1. d8> const greeting = readline();
  2. Welcome
  3. d8> greeting
  4. "Welcome"

Load external scripts

load() runs another JavaScript file in the current context, meaning that you can then access anything declared in that file.

  1. $ cat util.js
  2. function greet(name) {
  3. return 'Hello, ' + name;
  4. }
  5. $ d8
  6. d8> load('util.js');
  7. d8> greet('World!');
  8. "Hello, World!"

Pass flags into JavaScript

It’s possible to make command-line arguments available to your JavaScript code at runtime with d8. Just pass them after -- on the command line. You can then access them at the top-level of your script using the arguments object.

  1. out.gn/x64.optdebug/d8 -- hi

You can now access an array of the arguments using the arguments object:

  1. d8> arguments[0]
  2. "hi"

More resources

Kevin Ennis’s D8 Guide has really good information about exploring V8 using d8.

Background of the name d8: very early on, V8 had a “sample shell”, whose purpose was to demonstrate how V8 could be embedded to build a JavaScript shell. It was intentionally minimalist, and was simply called “shell”. Soon after, a “developer shell” was added with more convenience features to aid developers in their daily work, and it needed a name too. The original reason why “d8” was chosen as a name is lost to history; it caught on because “eveloper” is 8 omitted characters, so “d8 shell” makes sense as an abbreviation, and also fits in nicely with “V8” as the project name.