Insert cell
Insert cell
Insert cell
scopesExample = {
// if we ignore the globalScopeExample = { } above...
var imGlobal = "global variable"
function localScope() {
var imLocal = "local variable"
imGlobal // global can be accessed in here.
}
localScope()
// global variable can be accessed over here but not local variable
imGlobal
imLocal
}
Insert cell
Insert cell
locallyScopedFunctions = {
function globalFunction() {
function localFunction() {
return "Hai from local function"
}
return "Hai from global function"
}
globalFunction()
localFunction()
}
Insert cell
Insert cell
{ //I know this seems like a far-fetched example, but bookmark this notebook for when we get to React
function makeKidsGetIceCream() {
var kid = {
name: "Random Child",
iceCream: null,
getIceCream: function(flavor) {
this.iceCream = flavor
return this.name + " got " + this.iceCream + "-flavored ice cream!"
}
}
return kid.getIceCream
}
var iceCreamGetter = makeKidsGetIceCream()
return iceCreamGetter("chocolate")
}
Insert cell
Insert cell
{ //I know this seems like a far-fetched example, but bookmark this notebook for when we get to React
function makeKidsGetIceCream() {
var kid = {
name: "Random Child",
iceCream: null,
getIceCream: function(flavor) {
this.iceCream = flavor
return this.name + " got " + this.iceCream + "-flavored ice cream!"
}
}
return kid.getIceCream.bind(kid) //notice that .bind() actually creates a new function
}
var iceCreamGetter = makeKidsGetIceCream()
return iceCreamGetter("chocolate")
}
Insert cell
Insert cell
(param1, param2) => {
//code here
return 'something'
}
Insert cell
Insert cell
Insert cell
param => {
//code here
return 'something'
}
Insert cell
Insert cell
() => {
//code here
return 'something'
}
Insert cell
Insert cell
() => 'something'
Insert cell
Insert cell
func = function() {
return this.attr;
}
Insert cell
funcA = () => this.attr
Insert cell
objContext = ({
attr: 'objectAttribute',
objFunc: func,
objFuncA: funcA
})
Insert cell
objContext.objFunc()
Insert cell
objContext.objFuncA()
Insert cell
func.bind(objContext)()
Insert cell
funcA.bind(objContext)()
Insert cell
Insert cell
fortune = (type='random') => {
var fortunes = ['you will be happy', 'you will be sad', 'you will eat pineapple']
if(type==='happy')
return 'Your '+ type +' fortune: ' + fortunes[0]
else if(type==='sad')
return 'Your '+ type +' fortune: ' + fortunes[1]
else
return 'Your '+ type +' fortune: ' + fortunes[Math.floor(Math.random() * 3)]
}
Insert cell
fortune()
Insert cell
Insert cell
mysteryFunction = ({bob=5, joe=10, mary=30}={}) => {
//some long and complicated code nobody can read
return bob*joe*mary
}
Insert cell
mysteryFunction()
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