Public
Edited
Jan 28
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
thisYear = 2025
Insert cell
myBirthYear = 1996 // update with your birth year and observe what happens to the cell below!
Insert cell
thisYear - myBirthYear
Insert cell
Insert cell
Insert cell
Insert cell
<html>
<head>
<title>My title</title>
</head>
<body>
<h1>A heading</h1>
<a href="link.url">Link text</a>
</body>
</html>
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<svg width="70" height="70" style="border: 1px solid black">
<circle cx="35" cy="35" r="25" fill="green" stroke = "blue" stroke-width = "3"/>
</svg>
Insert cell
Insert cell
Insert cell
<svg width="160" height="28" style="border: 1px solid black">
<text x="50%" y="50%" font-size="17" text-anchor = "middle" dominant-baseline = "middle">Hello, World!</text>
</svg>
Insert cell
Insert cell
<svg width="160" height="160" style="border: 1px solid black">
<g style="transform: translate(30px, 30px);">
<rect x="0" y="0" width="100" height="100" fill="orange" />
<g fill = blue>
<circle cx="25" cy="25" r="20" />
<circle cx="75" cy="25" r="20" />
<circle cx="25" cy="75" r="20" />
<circle cx="75" cy="75" r="20" />
</g>
</g>
</svg>
Insert cell
Insert cell
Insert cell
lastYear = 2024 // one line code
Insert cell
repeatedString = { // code block with a return value stored as a global variable
let outputString = "";
for (let i = 0; i < 5; i++) {
outputString += "Goodbye " + lastYear + "! "; // we can use any global variable in a code block
}
return outputString;
}
Insert cell
repeatedString // we can access this global variable, but we cannot access the `outputString` variable from the above code block
Insert cell
Insert cell
x = 240 // a number assigned to a new variable named `x`
Insert cell
y = 7 // a number assigned to a new variable named `y`
Insert cell
courseNum = x + y // a number (calculated) assigned to a new variable named `courseNum`
Insert cell
deptCode = "INFO" // a string assigned to a new variable named `deptCode`
Insert cell
deptCode + " " + courseNum // concatenate strings and numbers with the `+` operator
// this works fine because JavaScript is a "weakly-typed" language
Insert cell
x > y // number comparison returns a boolean value
Insert cell
"a" > "b" // strings can be compared too
// this returns false because it evaluates the ASCII values of the letters, and "b" comes after "a"
Insert cell
1 < 2 && 3 > 4 // this is how you AND comparisons
Insert cell
1 < 2 || 3 > 4 // this is how you OR comparisons
Insert cell
null == undefined // both null and undefined are falsy values, so in this loose equality comparison they are equal
Insert cell
null === undefined // but in a strict equality comparison, they are different
Insert cell
Insert cell
function generateRandomNumber() {
return Math.floor(Math.random() * 100); // Math.random() returns a float between 0 and 1
// Math.floor() rounds a float down to the previous integer
}
Insert cell
generateRandomNumber() // try rerunning this cell several times, what does it print each time?
Insert cell
Insert cell
a1 = [] // initialize an empty array
Insert cell
rainbowColors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] // create an array of string elements
Insert cell
rainbowColors[0] // JavaScript uses zero-indexing
Insert cell
rainbowColors[rainbowColors.length - 1] // `.length` lets you access the length of any array
// Why do we have to do -1 here? Because of zero-indexing
Insert cell
Insert cell
snoopy = ({
name: "Snoopy",
age: 75,
type: "dog",
isSmart: true
})
Insert cell
Insert cell
snoopy.type // one way to access an object property
Insert cell
snoopy["type"] // another way to access an object property
Insert cell
Object.keys(snoopy) // This function prints just the property names of an object
// It's like the header of a CSV file that gives names to the columns
Insert cell
Object.values(snoopy) // This function prints just the property values of an object
// It's like one row of a CSV file
Insert cell
Insert cell
snoopy.favorites = ({ // set the snoopy object's "favorites" property to an object
color: "orange",
food: "root beer"
})
Insert cell
pets = [ // initialize an array of pet objects
snoopy,
{name: "Tom", age: 85, type: "cat", isSmart: false, favorites: {color: "blue", food: "ice cream"}},
{name: "Jerry", age: 85, type: "mouse", isSmart: true, favorites: {color: "green", food: "cheese"}}
]
Insert cell
Insert cell
[{candy: "gumdrop", color: "red"}]
Insert cell
Insert cell
myPet = ({
animal: "dog",
age: "8",
color: "white",
type: "husky"
})
Insert cell
// uncomment the code below to add your pet to the `pets` array
pets.push(myPet)
Insert cell
Insert cell
ages = [31, 28, 27, 29, 23, 22, 24, 28, 26, 34]
Insert cell
canRentCar = function(age) { // this function returns true if the passed-in age is greater than or equal to 25
return age >= 25;
}
Insert cell
// use the filter function to get the ages that meet the car rental age requirement
ages.filter(canRentCar)
Insert cell
// we can also pass in an "anonymous function" (i.e. unnamed) if this function isn't likely to be reused elsewhere
ages.filter(function(age) {
return age >= 25;
})
Insert cell
Insert cell
// we can also write the anonymous function using the "arrow function notation"
ages.filter((age) => age >= 25)
Insert cell
// we can also use the index parameter to specify how to filter the elements
// here we keep the elements whose indices are even numbers
ages.filter((age, i) => i % 2 == 0)
Insert cell
Insert cell
smartPets = pets.filter((pet) => pet.isSmart)
Insert cell
Insert cell
{
for (let i = 0; i < pets.length; i++) {
pets[i].raffle = generateRandomNumber(1, 100); // Assign a random raffle number between 1 and 100
}
}
Insert cell
Insert cell
{
let raffleWinners = pets.filter(
(pet) => pet.raffle >= 30 && pet.raffle <= 80
);

{
console.log("Raffle Winners:", raffleWinners);
raffleWinners;
}
}
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