Published
Edited
Oct 26, 2019
3 stars
Insert cell
Insert cell
Insert cell
// Any line starting with // is a comment.

/* Comments can also span multiple lines.
You can "open" a comment with /*
And then close it with */
Insert cell
Insert cell
Insert cell
// Adding two numbers together returns the result, so this is an "expression".
10 + 5
Insert cell
Insert cell
// This resolves as `1 + (2 * 3)` due to operator precedence.
// >> Add parenthesis below to change it to `(1 + 2) * 3` and see what happens.
1 + 2 * 3
Insert cell
Insert cell
// >> Change the 0.3 to 0.2 to change it to `0.1 + 0.2` and see what happens.
0.1 + 0.3
Insert cell
Insert cell
// Note that you need at least ES6 for template literals with `backticks`.
'You can use single quotes, ' + "or double quotes, " + `or backticks`
Insert cell
Insert cell
// Note that you need at least ES6 for template literals with `backticks`.
`This is a multi-line string (multiple lines of text) being displayed from the code cell.
Template litearals are a new addition of ES6.
They allow us to create multi-line strings.
We can also embed any value or expression as a string, like ${10 + 5}.`
Insert cell
Insert cell
// If you are not in ES6, you can concatenate strings with other values.
// The \n is a special character sequence meaning "new line".
"If you're not in ES6, you can still concatenate strings.\n" +
'Note the "new-line" characters at the end of each line.\n' +
'We can also embed any value or expression as a string, like ' + (10 + 5).toString();
Insert cell
Insert cell
// Here's how to define a function named `greet` that returns the string 'Hello!'.
// Defining a function does NOT actually run it, it just describes what to do.
function greet() {
return 'Hello!';
}
Insert cell
Insert cell
// Calling a function is an expression, so it results in a value.
// >> Delete the parentheses from `greet();` to change it to `greet;` and see what happens.
greet();
Insert cell
Insert cell
// We define a function named `add`, which receives two arguments (inputs): `a` and `b`.
function add(a, b) {
return a + b;
}
Insert cell
Insert cell
// When we call `add(10, 5)`, the value of argument `a` is set to 10, and `b` to 5.
// Then the function is run, it adds `a + b` (10 + 5 = 15) and returns the value 15.
// >> Change below to `add(10, -2.5);` and see what happens.
add(10, 5);
Insert cell
Insert cell
// If `a` and `b` are strings, the `a + b` does concatenation instead of addition.
// >> Change the second string into a number like
// `add('We can also use numbers ', 42);` and see what happens.
add('We can also ', 'concatenate strings!');
Insert cell
Insert cell
// Calculates the area of a dome in square feet.
function domeArea(radius) {
let pi = 3.1416;
return pi * radius ** 2; // the ** operator makes 2 an exponent.
}
Insert cell
// To calculate the area of a 30 foot dome.
domeArea(30);
Insert cell
Insert cell
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