Unlisted
Edited
May 14
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// numbers
10
Insert cell
10 + 4.3
Insert cell
Math.PI
Insert cell
'This is a string'
Insert cell
"This is also a string"
Insert cell
`The answer to life is ${21 * 2}`
Insert cell
random = Math.random()
Insert cell
`This is a random number: ${random}. It is ${random > 0.5 ? "greater than " : "less than "} 0.5`
Insert cell
true
Insert cell
!false
Insert cell
!1
Insert cell
!0
Insert cell
Insert cell
arr = ['red', 'white', 'green']
Insert cell
arr.push('blue')
Insert cell
arr[3]
Insert cell
arr[-1]
Insert cell
arr.slice(2, 4)
Insert cell
[[1, 2], [3, 4], [5, 6]].flat()
Insert cell
arr.indexOf('blue')
Insert cell
arr.indexOf('goldenrod')
Insert cell
arr.slice(2, 4)
Insert cell
Insert cell
// basic syntax
course = ({
department: 'CSC',
number: 477,
name: 'Scientific and Information Visualization'
})
Insert cell
course.department
Insert cell
course['department']
Insert cell
// Object.keys() and Object.values()
Object.keys(course)
Insert cell
Object.values(course)
Insert cell
// spread syntax
/*
{
department: 'CSC',
number: 477,
name: 'Scientific and Information Visualization',
roomNumber: '14-232b'
}
*/
Object.assign(course, { number: 4077, roomNumber: '14-232b' })
Insert cell
// spread syntax
({ ...course, roomNumber: '14-232b', number: 4077 })
Insert cell
Insert cell
// function syntax
function addTwo(a, b) {
return a + b
}
Insert cell
// arrow syntax
addTwoAlt = (a, b) => a + b;
Insert cell
max = (a, b) => a > b ? a : b;
Insert cell
to_string = n => n.toString()
Insert cell
addTwo(2, addTwoAlt(2, 5))
Insert cell
Insert cell
// functions are values; first-class functions
weather.find(obj => obj.month === 'March').temp
Insert cell
weather
.filter(obj => obj.precip > 0)
.map(obj => obj.month)
Insert cell
[1, 2, 3, 4].reduce((accumulator, current) => accumulator > current ? accumulator : current)
Insert cell
weather.every(obj => obj.precip > 0)
Insert cell
{
const result = []
for (const obj of weather) {
if (obj.precip > 0) {
result.push(obj.month)
}
}
return result
}
Insert cell
Insert cell
// won't deal with them too much directly, but worth knowing what they are
Insert cell
Insert cell
Insert cell
10
Insert cell
ten = 10
Insert cell
ten + 12
Insert cell
Insert cell
Insert cell
codeBlockValue = {
let s = ''; // variable can be re-assigned
for (let i = 0; i < 10; i++) {
s = s + ', ' + i;
}

return s.trim().replace(',', '');
}
Insert cell
Insert cell
a = b + 10 // This cell references "b", which is defined in the next cell and changes every 3 seconds
Insert cell
b = {
let val = 0; // "val" is LOCAL to this code block, and cannot be accessed in other cells.
while (true) {
// "yield" is like "return", except instead of giving up control after returning the value,
// this cell will "return" the value val++ and then come back to this line.
yield Promises.delay(3000, val++); // Wait 3 seconds, then increment val and yield the value.
}
}
Insert cell
Insert cell
Insert cell
viewof fontSize = Inputs.range([0.1, 2], { step: 0.1, value: 1, label: 'Font size' })
Insert cell
fontSize + 10
Insert cell
<div style='font-size: ${fontSize}em'>
Try dragging the slider back and forth! Then expand this cell and think about what's happening here.
</div>
Insert cell
Insert cell
Insert cell
For example, 2 + 3 is ${2 + 3}
Insert cell
randomNum = {
return Math.floor(Math.random() * (10));
}
Insert cell
Insert cell
Insert cell
Insert cell
// 👈🏽 Note that this is a JavaScript cell
html`<b>This is HTML text using Observable's facilities for <a href="https://observablehq.com/@observablehq/htl"><i>Tagged templates.</i></a></b>`
Insert cell
// Similarly for markdown
md`**This is Markdown content using Observable's facilities for [_Tagged templates_](https://observablehq.com/@observablehq/htl).**`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
${flag()} ${flag('pause')} ${flag('note')} ${flag('alert')} ${flag('custom', 'white', 'plum')}
Insert cell
Insert cell
// d3.range
Insert cell
Insert cell
Insert cell
// d3.mean, d3.sum, d3.deviation
Insert cell
// d3.extent
Insert cell
// d3.median, d3.quantile
Insert cell
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