Published
Edited
Feb 13, 2019
Insert cell
Insert cell
Insert cell
Insert cell
//JavaScript makes HTML pages more dynamic and interactive.
Insert cell
Insert cell
{
console.log("Hello world");
// or
var helloMit = "Hello MIT";
console.log(helloMit);
}
Insert cell
//console.log() is a function which log in the actual console, not in observable.console is a subset of your browser. what your write on the observable will show up in console, but what you write in console will not show up in observable.
// if want to clear the content in console: clear()
Insert cell
Insert cell
//similar to stata
Insert cell
/*this is a multiline comment*/
Insert cell
Insert cell
Insert cell
height = 500
Insert cell
height= 800
Insert cell
//you cannot define a variable numtiple times. If you change one part in the oberservable, everything in the note will be affected. Unless you define that only in a block.
//注意与其他语言不同,obervable并不是从上到下一个个跑命令。对同一个变量不能赋值两次。
Insert cell
Insert cell
Insert cell
dimension = ({height, width})
Insert cell
width = 480
Insert cell
Insert cell
// dimension = {1000, 960}
Insert cell
Insert cell
Insert cell
// This block defines a cumulative sum of numbers from 0 to 10
{
let x = 0;
for (let i = 1; i <= 10; ++i) {
x += i;
}
return x;
}
Insert cell
//you must assign a variable in order to retrieve it later.
Insert cell
firstNumber
Insert cell
// when trying to access to the variable outside of the scope, errors show up.
Insert cell
Insert cell
x
Insert cell
Insert cell
function running_total(max) {
let x = 0;
for (let i = 1; i <= max; ++i)
x += i;
return x;
}
//when first define a variable you need to use "let"
Insert cell
running_total(10)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const a = 5;
a = 6;
return a;
}
//you cannot change a constant over time
Insert cell
Insert cell
Insert cell
{
let a = 5;
a = 6
return a;
}
Insert cell
Insert cell
Insert cell
1 + 2
Insert cell
typeof (1 + 2)
Insert cell
1.0 + 2.5
Insert cell
typeof (1.0 + 2.5)
Insert cell
2 / 0
Insert cell
typeof (2 / 0)
Insert cell
"a" / 2
Insert cell
typeof ("a" / 2)
Insert cell
Insert cell
//useful for flow control
Insert cell
"hello, world"
Insert cell
typeof "hello, world"
Insert cell
"hello, " + "world"
Insert cell
Insert cell
name = "ycfan" // Fill your name in here!
Insert cell
`hello, world, it's ${name}`
//可以用 ${}引用之前定义的变量
Insert cell
typeof (`hello, world, it's ${name}`)
Insert cell
Insert cell
9 > 10
Insert cell
typeof (9 > 10)
Insert cell
10 == 10
Insert cell
Insert cell
//注意这里的and是两个&&
Insert cell
(9 > 10) && (10 == 10) // Evaluates to false AND true
Insert cell
(9 > 10) || (10 == 10) // Evaluates to false OR true
Insert cell
!(9 > 10) && (10 == 10) // Evaluates to NOT(false) AND true
Insert cell
!((9 > 10) || (10 == 10)) // Evaluates to NOT(false or true)
Insert cell
Insert cell
Insert cell
typeof (x)
Insert cell
Insert cell
{ let y
return typeof (y)
}
Insert cell
Insert cell
ra = ["one", 2, 3.0]
Insert cell
typeof ra
Insert cell
ra[0]
Insert cell
ra[3]
Insert cell
Insert cell
function addthese(a, b) {
return a+b;
}
Insert cell
typeof(addthese)
Insert cell
typeof(addthese(1, 4))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
"hello, world".toUpperCase()
Insert cell
Insert cell
directory = {
const data = {"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]};
return data
}
Insert cell
directory.employees
Insert cell
directory.employees[1]
Insert cell
directory.employees[1].firstName
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