// Adding two numbers together returns the result, so this is an "expression".
10+5
// 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
// >> Change the 0.3 to 0.2 to change it to `0.1 + 0.2` and see what happens.
0.1+0.3
// Note that you need at least ES6 for template literals with `backticks`.
'You can use single quotes, '+"or double quotes, "+`or backticks`
// 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}.`
// 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();
// 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.
functiongreet(){
return'Hello!';
}
// 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();
// We define a function named `add`, which receives two arguments (inputs): `a` and `b`.
functionadd(a,b){
returna+b;
}
// 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);
// 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!');
// Calculates the area of a dome in square feet.
functiondomeArea(radius){
letpi=3.1416;
returnpi*radius**2;// the ** operator makes 2 an exponent.
}
// To calculate the area of a 30 foot dome.
domeArea(30);
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.