Codebraid with JavaScript

Inline code

Run

Inline code with .cb-run gives raw stdout. While the language can be specified with .javascript, .js works as well.

`console.log(1 + 2);`{.js .cb-run}

3

Expression and inline notebook

Inline code with .cb-expr evaluates an expression and then inserts the raw output into the document, where it is interpreted as Markdown. Inline code with .cb-nb (nb is short for notebook) is similar, except output is shown verbatim.

`` `\$2^8 = ${2**8}\$` ``{.js .cb-expr}

28 = 256

`4*16`{.js .cb-nb}

64

Stderr

In the event of an error, inline code automatically shows stderr by default. This code is executed in its own session, inline_error, so that it does not impact other examples.

`console.logs(1 + 2);`{.js .cb-run session=inline_error}

<string>:1 console.logs(1 + 2); ^ TypeError: console.logs is not a function at Object.<anonymous> (<string>:1:9) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 Node.js v18.15.0

Block code

Run

Code blocks with .cb-run give raw stdout. There is continuity between code blocks so long as they are in the same session; variables persist.

```{.js .cb-run}
message = "Hello from *JavaScript!*";
```
```{.js .cb-run}
console.log(message);
```

Hello from JavaScript!

Notebook

Code blocks with .cb-nb show the code and also the verbatim stdout.

```{.js .cb-nb session=notebook}
function pows_of_two(start, end) {
    n = 2;
    x = start;
    while (x < end) {
        process.stdout.write(String(2**x) + ", ");
        x += 1;
    }
    console.log(2**end);
}
pows_of_two(1, 9);
pows_of_two(1, 15);
```
function pows_of_two(start, end) {
    n = 2;
    x = start;
    while (x < end) {
        process.stdout.write(String(2**x) + ", ");
        x += 1;
    }
    console.log(2**end);
}
pows_of_two(1, 9);
pows_of_two(1, 15);
2, 4, 8, 16, 32, 64, 128, 256, 512
2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768

Stderr

Code blocks show stderr automatically by default.

```{.js .cb-nb session=block_error}
x = 1 + ;
```
x = 1 + ;
source.js:1
x = 1 + ;
        ^

SyntaxError: Unexpected token ';'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.15.0