|
1 | 1 | # JavaScript |
2 | 2 |
|
3 | | -TODO... |
| 3 | +LiveCodes runs [JavaScript](https://developer.mozilla.org/docs/Web/JavaScript) (JS) in the browser. |
| 4 | +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. |
| 5 | + |
| 6 | +## Demo |
| 7 | + |
| 8 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 9 | + |
| 10 | +<LiveCodes template="javascript" height="60vh"></LiveCodes> |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +The JavaScript code is added as-is without any transformations to the [result page](../features/result.mdx) inside a `<script>` tag. |
| 15 | + |
| 16 | +If the code has imports or exports, the `<script>` tag will have `type="module"` attribute. |
| 17 | + |
| 18 | +The only case where the code is modified is when CommonJS `require`s are used to import external dependencies (not recommended - use ESM imports instead). |
| 19 | +These `require`s are converted to ESM imports with some glue code to make them work. |
| 20 | + |
| 21 | +Example: |
| 22 | + |
| 23 | +export const commonjs = `const { v4 } = require('uuid'); |
| 24 | +
|
| 25 | +document.body.innerHTML = v4();`; |
| 26 | + |
| 27 | +<LiveCodes params={{ activeEditor: 'script', js: commonjs, compiled: 'open' }}></LiveCodes> |
| 28 | + |
| 29 | +### External Modules |
| 30 | + |
| 31 | +Modules can be imported from various sources (e.g. npm, deno.land/x, jsr, CDNs, GitHub, etc.) including bare module imports. |
| 32 | +The modules are loaded from CDNs using importmaps (see [module resolution](../features/module-resolution.mdx) for details). |
| 33 | + |
| 34 | +Example: |
| 35 | + |
| 36 | +export const esm = `import _ from 'lodash'; |
| 37 | +
|
| 38 | +const arr = [1, 2, 3, 4, 5]; |
| 39 | +console.log(_.shuffle(arr)); // shuffle the array |
| 40 | +`; |
| 41 | + |
| 42 | +<LiveCodes params={{ activeEditor: 'script', js: esm, console: 'open' }}></LiveCodes> |
| 43 | + |
| 44 | + |
| 45 | +## Language Info |
| 46 | + |
| 47 | +### Name |
| 48 | + |
| 49 | +`javascript` |
| 50 | + |
| 51 | +### Alias |
| 52 | + |
| 53 | +`js` |
| 54 | + |
| 55 | +### Extensions |
| 56 | + |
| 57 | +`js`, `mjs` |
| 58 | + |
| 59 | +### Editor |
| 60 | + |
| 61 | +`script` |
| 62 | + |
| 63 | +### Compiler |
| 64 | + |
| 65 | +JavaScript runs natively in the browser without compilation. |
| 66 | + |
| 67 | +## Code Formatting |
| 68 | + |
| 69 | +Using [Prettier](https://prettier.io/). |
| 70 | + |
| 71 | +## Starter Template |
| 72 | + |
| 73 | +https://livecodes.io/?template=Javascript |
| 74 | + |
| 75 | +## Links |
| 76 | + |
| 77 | +- [JavaScript Reference on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript) |
| 78 | +- [Web API](https://developer.mozilla.org/en-US/docs/Web/API) |
0 commit comments