this will not work as intended in JavaScript:
return
<div>foo</div>;
it would be interpreted as:
return;
<div>foo</div>;
(automatic semicolon insertion)
versions that work:
return <div>foo</div>;
return (
<div>foo</div>
);
assigning the contents of an object or array to multiple variables simultaneously
long version (without destructuring):
const protocol = window.location.protocol;
const host = window.location.host;
const pathname = window.location.pathname;
version with destructuring assignment:
const { protocol, host, pathname } = window.location;
const [a, b, c] = [1, 2, 3];
const [result, logs] = someLongComputation();
// swapping values of a and b
[a, b] = [b, a];
named exports:
const foo = 1;
function bar() {
return 2;
}
export { foo, bar };
or
export const foo = 1;
export function bar() {
return 2;
}
named imports in another file:
import { foo, bar } from './mymodule.js';
when using a bundler like webpack:
import { foo, bar } from './mymodule';
there may be one default export per module:
export default function Main() {
return <div>Main</div>;
}
or
function Main() {
return <div>Main</div>;
}
export default Main;
imports (default and named):
import Main, { foo, bar } from 'mymodule.js';
the default can be imported under any name
notation for anonymous functions:
const multiply = (a, b) => {
return a * b;
};
short notation if the function only returns a single expression:
const multiply = (a, b) => a * b;
template strings allow for interpolation (via ${}
)
are delimited by backticks (`)
const message = `Hello, ${name}!`;