|
1 | 1 | # JavaScript |
2 | 2 |
|
3 | | -[JavaScript](https://developer.mozilla.org/docs/Web/JavaScript) is a versatile, interpreted programming language that powers the dynamic behavior on most websites. |
4 | | - |
5 | | -LiveCodes runs JavaScript natively in the browser. Since JavaScript runs in the browser, it has a host environment with access to the DOM and other Web APIs, but it does not have Node.js features such as the file system or process information. |
| 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. |
6 | 5 |
|
7 | 6 | ## Demo |
8 | 7 |
|
9 | 8 | import LiveCodes from '../../src/components/LiveCodes.tsx'; |
10 | 9 |
|
11 | | -export const params = { |
12 | | - template:"Javascript", |
13 | | - console:"open" |
14 | | -}; |
15 | | - |
16 | | -<LiveCodes params={params}></LiveCodes> |
| 10 | +<LiveCodes template="javascript" height="60vh"></LiveCodes> |
17 | 11 |
|
18 | 12 | ## Usage |
19 | 13 |
|
20 | | -LiveCodes executes JavaScript directly in the browser. |
| 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. |
21 | 17 |
|
22 | | -You can use it to test and demonstrate JavaScript code that interacts with the DOM, fetch APIs, or manipulate HTML elements dynamically. |
| 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. |
23 | 20 |
|
24 | | -### Loading Modules |
| 21 | +Example: |
25 | 22 |
|
26 | | -Most of the core functionality in JavaScript is available directly in the browser without any additional installation. |
| 23 | +export const commonjs = `const { v4 } = require('uuid'); |
27 | 24 |
|
28 | | -#### Standard Library |
| 25 | +document.body.innerHTML = v4();`; |
29 | 26 |
|
30 | | -JavaScript provides a standard set of built-in objects and functions which are available directly in the browser. |
| 27 | +<LiveCodes params={{ activeEditor: 'script', js: commonjs, compiled: 'open' }}></LiveCodes> |
31 | 28 |
|
32 | | -#### External Packages |
33 | | -To use additional functionality not included in the core language, you can import external packages (libraries) from CDN links. |
| 29 | +### External Modules |
34 | 30 |
|
35 | | -```js |
36 | | -import _ from 'https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js'; |
| 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 | 37 |
|
38 | 38 | const arr = [1, 2, 3, 4, 5]; |
39 | 39 | console.log(_.shuffle(arr)); // shuffle the array |
40 | | -``` |
| 40 | +`; |
| 41 | + |
| 42 | +<LiveCodes params={{ activeEditor: 'script', js: esm, console: 'open' }}></LiveCodes> |
41 | 43 |
|
42 | | -If you use JavaScript syntax extensions like JSX or JavaScript libraries/frameworks, you can select them directly in the programming language selection. |
43 | 44 |
|
44 | 45 | ## Language Info |
45 | 46 |
|
46 | 47 | ### Name |
47 | 48 |
|
48 | | -`Javascript` |
| 49 | +`javascript` |
49 | 50 |
|
50 | | -### Aliases |
| 51 | +### Alias |
51 | 52 |
|
52 | | -`js`, `node`, `ecmascript` |
| 53 | +`js` |
53 | 54 |
|
54 | 55 | ### Extensions |
55 | 56 |
|
56 | | -`js` |
| 57 | +`js`, `mjs` |
57 | 58 |
|
58 | 59 | ### Editor |
59 | 60 |
|
60 | 61 | `script` |
61 | 62 |
|
62 | 63 | ### Compiler |
63 | 64 |
|
64 | | -JavaScript runs natively in the browser — no compilation is required. |
65 | | - |
66 | | -### Version |
67 | | - |
68 | | -Depends on the browser's JavaScript engine and its supported ECMAScript features. |
| 65 | +JavaScript runs natively in the browser without compilation. |
69 | 66 |
|
70 | 67 | ## Code Formatting |
71 | 68 |
|
72 | | -Using [Prettier](https://prettier.io/) for code Formatting. You can format your code when saving the project, but you need to manually enable "Format on Save" in the settings. |
73 | | - |
74 | | -## Live Reload |
75 | | - |
76 | | -JavaScript code automatically reloads and executes when edited, allowing for real-time feedback. |
| 69 | +Using [Prettier](https://prettier.io/). |
77 | 70 |
|
78 | 71 | ## Starter Template |
79 | 72 |
|
80 | 73 | https://livecodes.io/?template=Javascript |
81 | 74 |
|
82 | 75 | ## Links |
83 | 76 |
|
84 | | -- [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) |
| 77 | +- [JavaScript Reference on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript) |
85 | 78 | - [Web API](https://developer.mozilla.org/en-US/docs/Web/API) |
0 commit comments