Operators: differences from JS
| Rook | JS | Note |
|---|---|---|
a == b |
a === b |
the language has no loose comparison |
a != b |
a !== b |
|
x == null |
x == null |
stays loose: catches both null and undefined |
a = b, a += b |
the same | only as a statement: f(a = 1), a = b = c are errors |
i++, i-- |
the same | only as a statement: a = i++ is an error; no ++i |
&&, ||, ! |
the same | only for bool; a default value is written with ?? |
a + b |
the same | numbers or strings; "a" + 1 is an error, use a template |
x as T |
x |
the types must be related, see below |
delete obj.key |
the same | only as a statement, see below |
The rest is as in JS: arithmetic, comparisons, bitwise operations, **, ??, ?., ? :,
typeof, instanceof, new, spread ..., indexing xs[i].
The language has no var, function, ===, while, do...while, for...in, with or comma
operator.
value as Typesees a value as another type, as in TypeScript, and leaves nothing in JS. The types must be related, one assignable to the other:element as HTMLInputElementandJSON.parse(text) as Configpass,count as stringis an error.asbinds like a comparison:a + b as number > cis((a + b) as number) > c. A variable namedasis still a name.delete obj.keyremoves a key of a map ({ [string]: T }), a field of type?T, or a field of anany; a required field cannot be deleted, and an array needssplice. It is a statement, also in handlers, and in a component it updates what reads the object.deleteis a keyword only at the start of a statement.
Only a function call or new can be a statement on its own, as in Go: a line a + b or
user.name by itself is an error. This catches typos and a line break before a binary operator:
const total = price
+ tax // error: there is already a ; after price, and "+ tax" does nothing