Published
Edited
Feb 13, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
console.log("Hello world");
// or
var helloMit = "Hello MIT";
console.log(helloMit);
}
Insert cell
Insert cell
Insert cell
//for multiline comments use three
/* (this is open )
hi
//in regular javascript can use tripple wutotes (''') and anything you put inside there will be your comments
/* (this is close)


Insert cell
Insert cell
Insert cell
height = 500
Insert cell
Insert cell
dimension = ({height, width})
Insert cell
width = 480
Insert cell
Insert cell
// dimension = {1000, 960}
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;
}

//block content will only be applied in a block, within the curly brackets . The blocks are a subset of the alrger project, as the console is a subset of observable .
Insert cell
Insert cell
x
//this variable is null because not within scope
Insert cell
Insert cell
function running_total(max) {
let x = 0;
for (let i = 1; i <= max; ++i)
x += i;
return x;
}
Insert cell
running_total(10)
Insert cell
Insert cell
Insert cell
Insert cell
// we get an error above because the variable firstNumber is not within the scope of observable.

Insert cell
Insert cell
Insert cell
/*I could have also done this

function addingNumbers (number1, number2){
let number3 = number1 + number2
return number3
}
*/


Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const a = 5;
a = 6;
return a;
}
Insert cell
//EB - there are multiple ways to create a new varible . Let, vars. const are ways to do this

Insert cell
//const can not be changed over time
Insert cell
{
let myfirstVar = 'hello'
const myConstantvar 'world'
myfirstVar = 'goodbye'
myConstantvar = hi
}

//note that I can change here the variable (myfirstVar) value that I have defined with let, but I could not change the variable (myConstantvar) that I defined with const
Insert cell
Insert cell
{
let a = 5;
a = 6
return a +30;
}
//here we are calling the variable within the curly brackets
//since we used let we were able to change the value of the variable (a)
Insert cell
Insert cell
//depending on the data type, they have different functionalities.
Insert cell
Insert cell
1 + 2
Insert cell
typeof (1 + 2)
// typeof() will tell you the datatype of the value you give it
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
"hello, world"
Insert cell
Insert cell
typeof "hello, world"
Insert cell
"hello, " + "world"
Insert cell
name = "Elizabeth" // Fill your name in here!
Insert cell
`hello, world, it's ${name}`
Insert cell
typeof (`hello, world, it's ${name}`)
Insert cell
'today is ${day}'
//NOT SURE IF I DID THIS RIGHT
Insert cell
Insert cell
Insert cell
9 > 10
Insert cell
typeof (9 > 10)
Insert cell
10 == 10
Insert cell
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
//undefined happens when we initiate a variable that we have not yet defined
//defined = assigned a value to it

Insert cell
Insert cell
ra = ["one", 2, 3.0]

//storinh values in a single bucket. can consist of multiple data types. including NA, undefined, etc.
//Next we will learn how to handle and play with arrays
Insert cell
Insert cell
Insert cell
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
//generators dont return an array, they return elements of an array instead
Insert cell
Insert cell
Insert cell
//to define an object (like the ones for the car above)
car = ({
make: 'fiat',
model : 500 }
)


// use the ( and { and : // this is observable specific

Insert cell
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
}

//a list of the employees at my company. Every element is an object upon itself.
Insert cell
Insert cell
Array (3) [
0: =
1:
2:
]
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