Public
Edited
Nov 14, 2023
3 forks
Importers
Insert cell
Insert cell
hello world
Insert cell
md`**hello world**`
Insert cell
html`<b>hello world</b>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
name = "John"
Insert cell
#### Number
Represents both integer and floating-point numbers.

Insert cell
age = 25;
Insert cell
Insert cell
isStudent = true;
Insert cell
typeof name
Insert cell
typeof age
Insert cell
typeof isStudent
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Number('123')
Insert cell
String(123)
Insert cell
parseInt(123.4) // base 10
Insert cell
parseInt('123.4', 10) // decimal part is truncated
Insert cell
+"123"
Insert cell
Insert cell
Insert cell
(1+2)*3/4
Insert cell
2**3 === Math.pow(2,3)
Insert cell
Math.min(1,2,3)
Insert cell
Math.max(1,2,3)
Insert cell
Math.random() // returns a random number greater than or equal to 0 and less than 1
Insert cell
Insert cell
Insert cell
`${name} is ${age}.` //template strings
Insert cell
Insert cell
Insert cell
colors = ["red", "green", "blue"];
Insert cell
mixedArray = [1, "Hello", true, {name: "John"}, () => "Function result"];
Insert cell
Insert cell
students = FileAttachment("tell_me_about_you@4.csv").csv()
Insert cell
Insert cell
myObject = ({name: "Paul", age: 25, city: "Boston"})
Insert cell
Insert cell
myObject.name === myObject["name"]
Insert cell
Insert cell
Insert cell
students[0] // the first array - indices starting at 0
Insert cell
students.length
Insert cell
columns = students["columns"]
Insert cell
columns.indexOf("Do you live in Massachusetts?") // returns the index of the first matching item
Insert cell
students.concat({"Timestamp: ": new Date()}) // combine values into a new array
Insert cell
```students.push({"Timestamp: ": new Date()}) // appending values into the existing array```
Insert cell
Insert cell
#### Convert Strings and Arrays
Insert cell
fruits = "apple,orange,pear"
Insert cell
fruitsArray = fruits.split(',')
Insert cell
newFruits = fruitsArray.join('/')
Insert cell
fruits.replaceAll(',', '/')
Insert cell
fruits.replace(',', '/') // only replace the first match
Insert cell
Insert cell
Insert cell
getRandomInt = (max) => (Math.floor(Math.random() * max) + 1)
// name of the function = (input) => return the output
Insert cell
Insert cell
Insert cell
getRandomInt(10) //returns integer 1-10
Insert cell
Insert cell
Insert cell
student_data = students.map((student, i) => ({...student, name: `student ${i+1}`}));

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(alphabet.slice(0,5), {x: "frequency", y: "letter", sort: {y: "x", reverse: true}}),
Plot.ruleX([0])
]
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
langData = {// Create an object to count language frequencies
const languageFrequencies = {};

// Iterate through the data and count frequencies
student_data.forEach(entry => {
const languages = entry["Please select all the coding languages you know of."].split(/[;,(]/);
languages.forEach(language => {
if (languageFrequencies[language]) {
languageFrequencies[language]++;
} else {
languageFrequencies[language] = 1;
}
});
});

// Convert the frequencies into an array of objects
const arrayOfObjects = Object.entries(languageFrequencies).map(([lang, count]) => ({
lang,
count: count.toString()
}));
return arrayOfObjects
}
Insert cell
Plot.plot({
marginLeft: 120,
marks: [
Plot.barX(langDataClean, {x: "count", y: "lang", sort: {y: "x", reverse: true}}),
Plot.ruleX([0])
]
})
Insert cell
Insert cell
langDataClean = langData.filter((el) => el.lang !== 'None' && el.lang.length < 20)
Insert cell
Insert cell
Insert cell
Plot.plot({
marginLeft: 100,
grid: true,
marks: [
Plot.dot(students_data_clean, {x: "name", y: "langs"})
]
})
Insert cell
Insert cell
Insert cell
students_data_flat = students_data_array.flatMap((student) => (student.langs.map((lan) => ({
name: student.name,
langs: lan
}))))
Insert cell
students_data_clean = students_data_flat.filter((el) => el.langs !== 'None' && el.langs.length < 20)
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more