Public
Edited
May 23
Insert cell
Insert cell
// Browser Console
// Open Chrome, right-click → Inspect → Console tab.
{
const num1 = 5; // var called num equal to 5
// num = 10; // This line will cause an error: uncomment only for testing (it is a const, cannot change over time)

let num2 = 5; // this can change
num2 = 10; // re-assignment
// 'console' is a variable in the browser, log is a property of the object that we're accessing.
console.log("num is:", num2);

var num3 = 5; // works as let but is used only in older codes, there are differences in scopes

for (let i = 0; i < 5; i++) {
console.log("i =", i);
}

console.log(4 < 5); // Expression that returns a Boolean value
console.log(4 > 5);

// In JS there are objects
// They can have properties inside them
const obj = {
num: 5
};
obj.num = "a"; // We can assign it to a new value, this works even is object is stored in a const. We are mutating the object, changing its internal values, without changing the object structure itself.
console.log("obj.num =", obj.num);
const object = {}; // You must define you variable, even empty.
object.num = 10; // If not defined you get a RuntimeError: object is not defined (reference error)
console.log("object.num =", object.num);

// Defining functions
const add = (a, b) => a + b; // more concise syntax -implicit return (if you don't open up the function body with {})
console.log(add(5, 10));
const add2 = (a, b) => {
a + b;
// aknckasv
// mkmk // if you have more lines of code to write
}
console.log(add2(5, 10));

function add3(a, b){
return a + b;
} // Old school way - still valid JS
}
Insert cell
// Example of object definition
{
const personExample = {
firstName: 'Jane',
lastName: 'Doe'
};
console.log(personExample);
console.log(personExample.firstName);
console.log(personExample.lastName);

function person1(first, last){
return {
first: first,
last: last
}
}
console.log("person1", person1('Jane', 'Doe'));

const person2 = (first, last) => {
return {
first: first,
last: last
};
}
console.log("person2", person2('Jane', 'Doe'));

const person3 = (first, last) => ({
first: first,
last: last
});
console.log("person3", person3('Jane', 'Doe'));

const person4 = (first, last) => ({
first, // if variable name matches property name, you just get rid of them
last
});
console.log("person4", person4('Jane', 'Doe'));

const person5 = (first, last) => ({first, last}); // the most concise way of writing this function
console.log("person5", person5('Jane', 'Doe'));
}
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