Published
Edited
Feb 20, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
//Need to put in var or else val is assumed to be constant
//Can't use let, because let is scoped only for the block, so we won't be able to call it outside of the for loop.
//But val also will not be defined outside of this broader block
for (var val = 0; val < 20; val++) {
console.log('Hello World +', val) //NOT ${val}' because that's for inserting into text in md
}
return val
}
Insert cell
{
for(var i=0; i<10; i++){
console.log(`Iteration number ${i}`); // Remember that the output will show up in the Console.
}
return i
}
Insert cell
Insert cell
{
let array = [0, 1, 2, 3, 4]
for(var i in array){
array[i] = array[i] ** 2;
}
return array;
}
Insert cell
Insert cell
Insert cell
{
//while not best to use in most cases...but definitely use for recursion
var counter = 0;
while(counter < 5){
console.log(`The counter is ${counter}, which is less than 5.`)
counter += 1
};
return counter
}
Insert cell
{
//does what while loop does but with a for.
for(var i = 1; i<5; i++){
console.log('accessing an array', i) //prints to array
}
//can't access i here anymore since it was declared within for loop only
}
Insert cell
Insert cell
Insert cell
//Link to JS comparisons notationi: https://www.w3schools.com/js/js_comparisons.asp

{
const num = 100;
if(num == 100) {
return "100!"
}
else {
return "Not 100..."
}
}
Insert cell
{
let test = 80
if(test<50 || test == 3){
return 'this is a small number'
} else if (test>100){
return 'this is a very large number'}
else{
return 'this is medium number'
}
}
Insert cell
{
const num = 100;
if(num == 99) {
return "99!"
}
else {
return "Not 99..."
}
}
Insert cell
Insert cell
{var number = 100;

if(number == 100){
return "Number is 100";
} else if(number < 100){
return "Number is less than 100.";
} else if(number > 100){
return "Number is greater than 100.";
}};

Insert cell
Insert cell

// function declaration with parameters
function multiply_this(a,b) {
return a*b;
}

Insert cell
{
// call the function, providing arguments for the parameters
const x = 4;
const y = 5;

return multiply_this(x,y); // run function with x and y as our arguments
}
Insert cell
Insert cell
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