Skip to content

Commit ef2e95b

Browse files
committed
Many improvements
1 parent 239a447 commit ef2e95b

File tree

13 files changed

+2953
-243
lines changed

13 files changed

+2953
-243
lines changed

examples/intro.ipynb

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# JavaScript Kernel Introduction\n",
8+
"\n",
9+
"This notebook demonstrates the basic features of the JupyterLite JavaScript kernel."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"## Basic Expressions\n",
17+
"\n",
18+
"The last expression in a cell is automatically returned (unless it ends with a semicolon):"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"1 + 2 + 3"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"\"Hello, \" + \"JavaScript!\""
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"// Adding a semicolon suppresses output\n",
46+
"const x = 42;"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"x * 2"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Variables Persist Across Cells\n",
63+
"\n",
64+
"Variables defined in one cell are available in subsequent cells:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"const greeting = \"Hello\";\n",
74+
"const name = \"World\";"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"`${greeting}, ${name}!`"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"## Functions"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"function factorial(n) {\n",
100+
" if (n <= 1) return 1;\n",
101+
" return n * factorial(n - 1);\n",
102+
"}"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": null,
108+
"metadata": {},
109+
"outputs": [],
110+
"source": [
111+
"factorial(10)"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"// Arrow functions\n",
121+
"const square = x => x * x;\n",
122+
"const numbers = [1, 2, 3, 4, 5];\n",
123+
"numbers.map(square)"
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"## Console Output\n",
131+
"\n",
132+
"`console.log()` and `console.error()` output is captured:"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"metadata": {},
139+
"outputs": [],
140+
"source": [
141+
"console.log(\"This is a log message\");\n",
142+
"console.log(\"Multiple\", \"arguments\", \"work\", \"too\");"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"console.error(\"This is an error message\");\n",
152+
"console.warn(\"This is a warning\");"
153+
]
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"## Async/Await\n",
160+
"\n",
161+
"Top-level await is supported:"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"const delay = ms => new Promise(resolve => setTimeout(resolve, ms));\n",
171+
"\n",
172+
"console.log(\"Starting...\");\n",
173+
"await delay(1000);\n",
174+
"console.log(\"Done after 1 second!\");"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"// Async function example\n",
184+
"async function fetchData() {\n",
185+
" await delay(500);\n",
186+
" return { status: \"success\", data: [1, 2, 3] };\n",
187+
"}\n",
188+
"\n",
189+
"await fetchData()"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"## Classes"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"class Point {\n",
206+
" constructor(x, y) {\n",
207+
" this.x = x;\n",
208+
" this.y = y;\n",
209+
" }\n",
210+
" \n",
211+
" distance(other) {\n",
212+
" const dx = this.x - other.x;\n",
213+
" const dy = this.y - other.y;\n",
214+
" return Math.sqrt(dx * dx + dy * dy);\n",
215+
" }\n",
216+
" \n",
217+
" toString() {\n",
218+
" return `Point(${this.x}, ${this.y})`;\n",
219+
" }\n",
220+
"}"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"const p1 = new Point(0, 0);\n",
230+
"const p2 = new Point(3, 4);\n",
231+
"p1.distance(p2)"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"## Error Handling\n",
239+
"\n",
240+
"Errors are displayed with stack traces:"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"metadata": {},
247+
"outputs": [],
248+
"source": [
249+
"function outer() {\n",
250+
" inner();\n",
251+
"}\n",
252+
"\n",
253+
"function inner() {\n",
254+
" throw new Error(\"Something went wrong!\");\n",
255+
"}\n",
256+
"\n",
257+
"outer()"
258+
]
259+
}
260+
],
261+
"metadata": {
262+
"kernelspec": {
263+
"display_name": "JavaScript",
264+
"language": "javascript",
265+
"name": "javascript"
266+
},
267+
"language_info": {
268+
"file_extension": ".js",
269+
"mimetype": "text/javascript",
270+
"name": "javascript",
271+
"version": "ES2021"
272+
}
273+
},
274+
"nbformat": 4,
275+
"nbformat_minor": 4
276+
}

0 commit comments

Comments
 (0)