Published
Edited
Mar 18, 2019
1 fork
Insert cell
Insert cell
Insert cell
function bob(a,b)
{
var c = a%2
if (a===b)
{
return function sam()
{
if(c===b)
return function mike(a,b)
{
return a+b
}
return c
}
}
else if (a==b)
{
if(a===true)
return a;
else
return function zzz(s,l,e,p)
{
return s + "zz" + l + "zz" + e + "zz" + e + "zz" +p
}
}
else
return bob(a+1,b)
}
Insert cell
Insert cell
md`
Insert cell
Insert cell
{
var a = 1;
// ...a thousand lines of code here
var a = 2; // doesn't throw a duplicate declaration error
return a; // a seems to be changed
}
Insert cell
{
let a = 1;
let a = 2; // duplicate declaration error
}
Insert cell
Insert cell
{
const A = 2
A++ // constant assignment error
return A
}
Insert cell
Insert cell
{
while(false) /* the block --> */ { /* some JS statements here */ }
if(true)
{
// inside the block
}
}
Insert cell
// blocks (with a lot more features added) are actually used in this notebook
{ /* JS statements in here */ }
Insert cell
Insert cell
{
// outside of block scope
if(true) {
// inside of block scope
var a = 2
}
return a; // yes it would
}
Insert cell
Insert cell
{
return b
}
Insert cell
{
// what you see:
if(false) {
var b = 2
}
return b // why no error?
}
Insert cell
{
// what JavaScript sees:
var b // <-- hoisted variable declaration outside the block scope
if(false) {
b = 2
}
return b
}
Insert cell
{
b = 2 // notebook doesn't like assigning globally scoped variables
}
Insert cell
{
b = 2
return b // why no error?
var b
}
Insert cell
Insert cell
{
b = 2
return b
let b // unreachable statement
}
Insert cell
{
// outside block scope
if(true) {
let b = 2 // inside block scope
}
return b
}
Insert cell
Insert cell
function doSequential(fn1, fn2, a) {
return fn2(fn1(a))
}
Insert cell
// we can put two functions in as parameters in a one-liner. Can't do that in Java
doSequential( x=>x+1, x=>x*5, 5)
Insert cell
Insert cell
{
const dog = {
sound: "woof",
makeBarker: function() {
return function() {
return this.sound
}
},
makeBarkerArrow: function() {
return () => this.sound // <-- `this` is bound the the `this` of the function
}
}
const bark = function() { return this.sound }
const barker = dog.makeBarker()
const arrowFnBarker = dog.makeBarkerArrow()
return [
bark.call(dog), //
barker.call(dog), // must be bound or called with dog
arrowFnBarker() // already bound
]
}
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