Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions examples/intro.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JavaScript Kernel Introduction\n",
"\n",
"This notebook demonstrates the basic features of the JupyterLite JavaScript kernel."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Expressions\n",
"\n",
"The last expression in a cell is automatically returned (unless it ends with a semicolon):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 + 2 + 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"Hello, \" + \"JavaScript!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Adding a semicolon suppresses output\n",
"const x = 42;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables Persist Across Cells\n",
"\n",
"Variables defined in one cell are available in subsequent cells:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const greeting = \"Hello\";\n",
"const name = \"World\";"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"`${greeting}, ${name}!`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function factorial(n) {\n",
" if (n <= 1) return 1;\n",
" return n * factorial(n - 1);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"factorial(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Arrow functions\n",
"const square = x => x * x;\n",
"const numbers = [1, 2, 3, 4, 5];\n",
"numbers.map(square)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Console Output\n",
"\n",
"`console.log()` and `console.error()` output is captured:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"console.log(\"This is a log message\");\n",
"console.log(\"Multiple\", \"arguments\", \"work\", \"too\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"console.error(\"This is an error message\");\n",
"console.warn(\"This is a warning\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Async/Await\n",
"\n",
"Top-level await is supported:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const delay = ms => new Promise(resolve => setTimeout(resolve, ms));\n",
"\n",
"console.log(\"Starting...\");\n",
"await delay(1000);\n",
"console.log(\"Done after 1 second!\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Async function example\n",
"async function fetchData() {\n",
" await delay(500);\n",
" return { status: \"success\", data: [1, 2, 3] };\n",
"}\n",
"\n",
"await fetchData()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Classes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Point {\n",
" constructor(x, y) {\n",
" this.x = x;\n",
" this.y = y;\n",
" }\n",
" \n",
" distance(other) {\n",
" const dx = this.x - other.x;\n",
" const dy = this.y - other.y;\n",
" return Math.sqrt(dx * dx + dy * dy);\n",
" }\n",
" \n",
" toString() {\n",
" return `Point(${this.x}, ${this.y})`;\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const p1 = new Point(0, 0);\n",
"const p2 = new Point(3, 4);\n",
"p1.distance(p2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Error Handling\n",
"\n",
"Errors are displayed with stack traces:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function outer() {\n",
" inner();\n",
"}\n",
"\n",
"function inner() {\n",
" throw new Error(\"Something went wrong!\");\n",
"}\n",
"\n",
"outer()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "JavaScript",
"language": "javascript",
"name": "javascript"
},
"language_info": {
"file_extension": ".js",
"mimetype": "text/javascript",
"name": "javascript",
"version": "ES2021"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Loading