Published
Edited
Feb 24, 2022
Insert cell
# Functions
Insert cell
{
//named function
function f1(a, b) {
return a+b;
}
// anonymous
const f2 = function(a, b){
return a-b;
};
const ug = (a,b) => a*b;
}
Insert cell
{
const ab = (a,b) => ({a,b});
return ab(1, 2);

}
Insert cell
j = {
function Person(_name) {
let name = _name,
votes = 0,
city = "SF";

const getName = function () {
console.log(`I'm ${name}`);
return name;
};

const publicObject = {};
publicObject.getName = getName;

publicObject.vote = () => {
votes += 1;
return votes;
};

//here implement getCity and setCity

function getCity() {}
function setCity() {}

publicObject.getCity = getCity;
publicObject.setCity = setCity;
return publicObject;
}

const j = Person("John");

j.setCity("Pereira");

return j.getCity();
}
Insert cell
{
function add3(a){
return a + 3;
}

function add5(a){
return a + 5;
}

function metaAdd(a){
return function (b){
return a + b;
};
}

return metaAdd(2)(4);

}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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