Published
Edited
Oct 9, 2018
1 fork
2 stars
Insert cell
Insert cell
Insert cell
answer = 2 * 3 * 7
Insert cell
Insert cell
({"subjects": ["life", "the universe", "everything"]})
Insert cell
Insert cell
{
let sum = 0;
for (let i = 1; i <= 10; ++i) sum += i;
return sum;
}
Insert cell
Insert cell
Insert cell
42, // Oops, a SyntaxError!
Insert cell
Insert cell
Insert cell
document.createTextNode("Hello, I am text!")
Insert cell
Insert cell
html`Hello, I am <i>italicized</i>!`
Insert cell
Insert cell
md`Hello, I am *Markdown*!`
Insert cell
Insert cell
html`<svg width=50 height=50>
<circle cx=25 cy=25 r=20 fill=red></circle>
</svg>`
Insert cell
Insert cell
{
const canvas = document.createElement("canvas");
canvas.width = 50;
canvas.height = 50;
const context = canvas.getContext("2d");
context.arc(25, 25, 20, 0, 2 * Math.PI);
context.fillStyle = "blue";
context.fill();
return canvas;
}
Insert cell
Insert cell
Insert cell
x = 2
Insert cell
y = 3
Insert cell
Insert cell
x + y
Insert cell
Insert cell
{
const x = 1337; // This defines a local x, masking the cell above.
return x;
}
Insert cell
Insert cell
z * z
Insert cell
Insert cell
z = 8
Insert cell
Insert cell
doop = 1
Insert cell
doop = 2 // doop is not a unique name! 💥
Insert cell
doop + 1 // doop is not defined 💥
Insert cell
Insert cell
a = b + 1 // a depends on b
Insert cell
b = a + 1 // b depends on a 💥
Insert cell
Insert cell
Insert cell
function k(x, y) {
return x / y;
}
Insert cell
Insert cell
Insert cell
message = new Promise(resolve => {
setTimeout(() => {
resolve("I am asynchronous!");
}, 3000);
})
Insert cell
Insert cell
message.toUpperCase()
Insert cell
Insert cell
Insert cell
miserables = {
const response = await fetch("https://gist.githubusercontent.com"
+ "/mbostock/4062045"
+ "/raw/94d1b56d1e6c0d4bcc1edd86a41385a14ed4c1b3"
+ "/miserables.json");
return response.json();
}
Insert cell
Insert cell
Insert cell
function* range(n) {
for (let i = 0; i < n; ++i) {
yield i;
}
}
Insert cell
Insert cell
button, range(100)
Insert cell
viewof button = html`<button>Restart</button>`
Insert cell
Insert cell
{
for (let i = 0; i < 100; ++i) {
yield i;
}
}
Insert cell
Insert cell
seconds = {
let i = -1;
while (true) {
yield new Promise(resolve => {
setTimeout(() => {
resolve(++i);
}, 1000);
});
}
}
Insert cell
Insert cell
{
let i = -1;
while (true) {
yield Promises.delay(1000, ++i);
}
}
Insert cell
Insert cell
Generators.observe(change => {
let i = -1;
let interval = setInterval(() => change(++i), 1000);
return () => clearInterval(interval);
})
Insert cell
Insert cell
input = html`<input type=range>`
Insert cell
Generators.observe(change => {
const listener = () => change(input.valueAsNumber);
input.addEventListener("input", listener);
change(input.valueAsNumber); // Report the initial value.
return () => input.removeEventListener("input", listener);
})
Insert cell
Insert cell
hue = {
const form = html`<form><table>
<tr><td>
<input name=in type=range min=0 max=359 value=180 step=1>
<output name=out></output>° hue
</td></tr>
</form>`;
form.oninput = () => form.value = form.out.value = +form.in.value;
form.oninput(); // Set the initial value.
return form;
}
Insert cell
Generators.input(hue)
Insert cell
Insert cell
viewof foo = html`<input type=range min=0 max=1 step=any>`
Insert cell
foo
Insert cell
Insert cell
(viewof foo).tagName
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more