Published
Edited
Feb 8, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
myNumber = 1
Insert cell
myString = "Hello world!"
Insert cell
myOtherString = myString
Insert cell
Insert cell
Insert cell
1 < 2
Insert cell
"a" > "b" // Why? Explain
Insert cell
'2' > 1 // Comparison of different types
Insert cell
1 < 2 && 3 > 4
Insert cell
1 < 2 || 3 > 4
Insert cell
Insert cell
array1 = new Array()
Insert cell
array2 = [] // Another way of creating an array
Insert cell
array3 = ["Steve Jobs", "Bill Gates", "Jeff Bezos"]
Insert cell
// Arrays can contain different data types
array4 = ["Steve Jobs", 55, true]
Insert cell
// Accessing arrays, arrays in JavaScript are 0-indexed
array4[0]
Insert cell
Insert cell
array5 = [0, 1, 2, 3, 4, 5] // Initialise the array
Insert cell
// Slice from left
array5.slice(2)
Insert cell
// Slice from right
array5.slice(-2)
Insert cell
// Slice from both ends, first index inclusive
array5.slice(0, 2)
Insert cell
// Turn an array into a string
array5.join('\n')
Insert cell
// Append to the end of an array - note that this DOES NOT change the original!
array5.concat(6)
Insert cell
// Remove from end of array - note that this mutates the original!
array5.pop()
Insert cell
Insert cell
object1 = new Object() // "Object constructor"
Insert cell
object2 = {} // "Object literal"
Insert cell
object3 = ({
name: "Jeff Bezos",
age: 55,
netWorth: "filthy rich"
})
Insert cell
Insert cell
Insert cell
Insert cell
object3.netWorth = "very filthy rich"
Insert cell
object3
Insert cell
Insert cell
object3["netWorth"]
Insert cell
// In other words...
object3.netWorth == object3["netWorth"]
Insert cell
object3["netWorth"] = "extremely filthy rich"
Insert cell
object3
Insert cell
Insert cell
Insert cell
{
let x = 0;
for (let i = 1; i <= 5; ++i) {
x += i; // adding numbers from 1 - 5
}
return x;
}
Insert cell
Insert cell
function showMessage() {
console.log("Hi")
}
Insert cell
Insert cell
showMessage()
Insert cell
Insert cell
Insert cell
function newFunction() {
let myVariable = 3
myVariable = myVariable + 1
return myVariable
}
Insert cell
newFunction() // 1 + 3
Insert cell
myVariable = 10
Insert cell
newFunction() // Still returns 4! Because it uses the local variable.
Insert cell
Insert cell
Insert cell
function add(a, b) {
return a + b
}
Insert cell
add(1, 2)
Insert cell
Insert cell
outputFromFunction = add(1,2)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
function (a, b) {
return a + b
}(1,2)
Insert cell
Insert cell
Insert cell
myNamedAddFunction = (a, b) => {
return a + b
}
Insert cell
myNamedAddFunction(1, 2)
Insert cell
Insert cell
Insert cell
// Here, we download some data on Covid-19 in San Francisco
df = FileAttachment("cars.csv").csv({typed: true}) // Dataframe
Insert cell
Inputs.table(df.slice(0, 10))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
carsDf = FileAttachment("cars.csv").csv({typed: true})
Insert cell
Inputs.table(carsDf)
Insert cell
Insert cell
Insert cell
Plot.dot(carsDf,
{
x: "economy (mpg)",
y: "power (hp)"
})
.plot()
Insert cell
Insert cell
Plot.dot(carsDf,
{
x: d => (d["weight (lb)"] * 0.425144),
y: d => d["power (hp)"]
})
.plot()
Insert cell
Insert cell
Plot.dot(carsDf,
{
x: d => d["weight (lb)"],
y: d => d["power (hp)"]
})
.plot()
Insert cell
Insert cell
Plot.dot(carsDf, {x: d => d["weight (lb)"] * 0.425144, y: d => d["power (hp)"]})
.plot({
x: {
grid: true,
label: "Weight (kg)",
labelAnchor: "center"
},
y: {
grid: true,
label: "Power (hp)",
labelAnchor: "center"
}
})
Insert cell
Insert cell
Insert cell
Plot.dot(carsDf, {x: d => d["weight (lb)"], y: d => d["power (hp)"]})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.dot([[3750, 200],
[3864, 250],
[3901, 304]]).plot()
Insert cell
Insert cell
Insert cell
Plot.dot([[3750, 200],
[3864, 250],
[3901, 304]])
.plot({
x: {
grid: true,
domain: [3500, 5000]
},
y: {
grid: true,
domain: [80, 500]
}
})
Insert cell
Insert cell
irisDf = FileAttachment("iris.csv").csv({typed: true})
Insert cell
Inputs.table(irisDf)
Insert cell
/* Enter Your Code Below */
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.dot([[3750, 200], [3864, 250], [3901, 304]])
.plot({
x: {
grid: true,
domain: [3500, 5000]
},
y: {
grid: true,
domain: [80, 500]
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.dot(carsDf, {x: "economy (mpg)", y: "power (hp)"}),
Plot.ruleX([30]), // a rule at x = 0, x= 80
Plot.ruleY([60])
],
x: {
grid: true,
domain: [0, 100]
},
y: {
grid: true,
domain: [0, 250]
}
})
Insert cell
Insert cell
Insert cell
irisMeanDf = FileAttachment("iris_mean.csv").csv({typed: true})
Insert cell
Inputs.table(irisMeanDf)
Insert cell
Plot.barX(irisMeanDf,
{
x: "petal.width",
y: "variety"
})
.plot()
Insert cell
Insert cell
Plot.barX(irisMeanDf,
{
x1: 0,
x2: "petal.width",
y: "variety"
})
.plot(
{
x: {
labelAnchor: "center"
},
y: {
labelAnchor: "top",
}
}
)
Insert cell
Insert cell
Plot.barY(irisMeanDf,
{
y1: 0,
y2: "petal.width",
x: "variety"
})
.plot({
y: {
labelAnchor: "top",
}
}
)
Insert cell
Insert cell
Plot.barX(irisMeanDf,
{
x1: 0,
x2: "petal.width",
y: "variety",
fillOpacity: 0.5
})
.plot(
{
y: {
labelAnchor: "top"
}
}
)
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(irisMeanDf, {x: "petal.width", y: "variety", fill: "salmon"}),
Plot.ruleX([0, 1.2])
],
y: {
labelAnchor: "top"
}
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(irisMeanDf, {x: "petal.width", y: "variety", fill: "variety"}),
Plot.ruleX([0])
],
y: {
labelAnchor: "top"
}
})
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(irisMeanDf,
{
x: "petal.width",
y: "variety",
fill: d => d["petal.width"] > 1.4 ? "salmon" : "steelblue"
}),
Plot.ruleX([0])
]
})
Insert cell
Insert cell
Insert cell
Plot.plot({
color: {
domain: ["small", "big"],
range: ["salmon", "steelblue"],
legend: true
},
marks: [
Plot.barX(irisMeanDf,
{
x: "petal.width",
y: "variety",
fill: d => d["petal.width"] > 1.4 ? "small" : "big"}),
Plot.ruleX([0])
],
y: {
labelAnchor: "top"
}
})
Insert cell
Insert cell
Insert cell
tempDf = FileAttachment("temp_anomalies_data.csv").csv({typed: true})
Insert cell
/* Enter your code below */
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