Skip to main content

JavaScript

LiveCodes runs JavaScript (JS) in the browser. Accordingly, it has access to the DOM and other Web APIs, but it does not have access to Node.js APIs such as the file system or process information.

Demo

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "javascript"
};
createPlayground('#container', options);

Usage

The JavaScript code is added as-is without any transformations to the result page inside a <script> tag.

If the code has imports or exports, the <script> tag will have type="module" attribute.

The only case where the code is modified is when CommonJS requires are used to import external dependencies (not recommended - use ESM imports instead). These requires are converted to ESM imports with some glue code to make them work.

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"activeEditor": "script",
"js": "const { v4 } = require('uuid');\n\ndocument.body.innerHTML = v4();",
"compiled": "open"
}
};
createPlayground('#container', options);

External Modules

Modules can be imported from various sources (e.g. npm, deno.land/x, jsr, CDNs, GitHub, etc.) including bare module imports. The modules are loaded from CDNs using importmaps (see module resolution for details).

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"activeEditor": "script",
"js": "import _ from 'lodash';\n\nconst arr = [1, 2, 3, 4, 5];\nconsole.log(_.shuffle(arr)); // shuffle the array\n",
"console": "open"
}
};
createPlayground('#container', options);

Language Info

Name

javascript

Alias

js

Extensions

js, mjs

Editor

script

Compiler

JavaScript runs natively in the browser without compilation.

Code Formatting

Using Prettier.

Starter Template

https://livecodes.io/?template=Javascript