Basic syntax is very similar to Java/C
if (cond) { stmt; } else if (cond) { stmt; }
switch (a) { case 1: break; ...; default: ...; }
while (i < 0) { stmt; }
for (i=0; i < 10; i++) { stmt; }
for (e of array) { stmt; }
try { throw 1; } catch (e) { stmt; } finally { stmt; }
+, --, %, …~, &, |, ^, …!, &&, ||==/!= true if operands have the same value (after type conversion)===/!== true only if operands have the same value and type (no automatic type conversion) console.log(3 == "3");
console.log(3 === "3");
==/=== is true only if both
operands reference the same object
_, and $
$ and _let name=value;
let x=10;let declaration
const n = 42;
var was used instead of let
var laterfunction func_name(parameter1, parameter2,...)
{
... function body ...
return value;
}
let a = 10; // a is number type
a = "good"; // a is string type now
typeof operator returns the current type of the variablea = "good";
typeof a;
numberstringbooleanbigintsymbolnull)undefined)number Type&, |, ^, >>, <<) convert a number to a 32-bit integer
NaN and Infinity are valid numbersbigint (64-bit integer) was added to ES2020 as a primitive type
n behind the number. eg) 12nnumber with bigint. eg) 12 + 12n not allowedboolean Typetrue or falseNaN, “”, null, undefined[] or object {} are NOT falsy values (more on these later)string Type'John' or "John"length property returns the length of the stringcharAt(), substring(), indexOf(), …let a = "abcdef";
b = a.substring(1, 4); // b = "bcd"
let s = `this is template literal
that can include ${expression}!`;
`...`${expression} is evaluated and replaced: ${1+2} → 3`, $, {, }, escape them like \$undefined and null Typeundefined: type of the value undefined
null: type of the value null
null if no object can be returnedtypeof null returns object (!)undefined and null are often interchangeably used, but they are different in principleundefined == null; // true
undefined === null; // false
symbol Typesymbol type is mainly used to create a unique identifier
Example
let Sym1 = Symbol("Sym");
let Sym2 = Symbol("Sym");
console.log(Sym1 == Sym2); // returns "false"
// Symbols are guaranteed to be unique
console.log(Sym1.description) // returns "Sym"
numbers and string are automatically type converted to each other
"3"*"4"=121+"2"="12"Number(), String(), Boolean(), parseFloat(), parseInt(), …let o = { x: 1, y: "good" };
let c = o.x + o["y"];
o["x"] is identical to o.xlet o = { x: 1, y: 2, z:{ x: 3, y: 4 } };
let o = new Object();
o.x = 10;
o.y = 30;
delete o.x;
Object.keys(o);
let o = { x: 10, y: 20 };
let p = o;
o.x = 30;
console.log(p.x);
let o = { x: 10 };
let p = { x: 10 };
console.log(o == p);
new Array(), or [ 1, 2, 3 ]length property returns the size of the array
let a = new Array(1, 2, 3);
let b = [1, 2];
console.log(a.length);
let a = new Array();
a[0] = 3;
a[2] = "string";
let b = [1, "good", , [2, 3] ];
console.log(a.length);
undefinedRegExp is a special object that describes a pattern to search for in a stringlet r = /a?b*c/;
String: search(), match(), replace(), split()RegExp: exec(), test()/ABC/.test(str); // true if str has substr ABC
/ABC/i.test(str); // i ignores case
/[Aa]B[C-E]/.test(str);
'123abbbc'.search(/ab*c/); // 3 (position of 'a')
'12e34'.search(/[^\d]/); // 2 [^x]: except x, \d: digit
try {
throw new Error("I am thrown off!");
} catch (err) {
console.log(err.message);
} finally {
console.log("Finally, I am here");
}
thrown
Error object is most common because stacktrace is available: Error.stack[{ "x": 3, "y": "Good" }, { "x": 4, "y": "Bad" }]
JSON.stringify(obj)
JSON.parse(str)
let x = '[{ "x": 3, "y": "Good" }, { "x": 4, "y": "Bad" }]';
let o = JSON.parse(x);
let n = o[0].x + o[1].x;
console.log(n);