Slide navigation: Forward with space bar, →, or PgDn. Backwards with ← or PgUp.
6 * 7
const number = 6 * 7; console.log(6 * 7);
node
REPL.
6 * 7; // Not useful
console.log(6 * 7); Useful because console.log
has a side effect
break
, continue
, return
, throw
)}
or the end of input.const a = x
+ someComplicatedFunctionCall() // +
not offending—no semicolon
const b = y; // const is offending—semicolon placed before it
const x = a (console.log(6 * 7))
a(console.log(...))
++
, --
, or =>
immediately preceded by line terminatorbreak
, continue
, return
, throw
, or yield
immediately followed by line terminatorreturn // Semicolon inserted here someComplicatedExpression;
=== strictly equal to !== not strictly equal to
!==
yundefined
and null
only equal to themselves.const harry = { name: 'Harry Smith', age: 42 } const harry2 = harry harry === harry2 //true
—two references to the same object const harry3 = { name: 'Harry Smith', age: 42 } harry === harry3 //false
—different objects
==
and !=
< less than <= less than or equal > greater than >= greater than or equal
'Hello' < 'Goodbye' // false 'Hello' < 'Hi' // true
'42' < 5 // Don't do this!
'42' < 5 //false
—'42'
is converted to the number42
'' < 5 //true
—''
is converted to the number0
'Hello' < 5 //false
—'Hello'
is converted toNaN
[42] < 5 //false
—[42]
is converted to the number42
[1, 2, 3] < {} //true
—[]
is converted to'1,2,3'
,{}
to'[object Object]'
'' == 0 //true
—''
is converted to 0. '0' == 0 //true
—'0'
is converted to 0. '0' == false //true
—both are converted to 0. null == false //false
—null
is only equal to itself andundefined
. '' == '0' //false
—no conversion since both operands are strings.
if (...)
—need not be Boolean values.NaN
, null
, undefined
, ''
are falsish.&&
, ||
, and !
, first operand of ? :
if (performance) { // Skipped ifperformance
isundefined
}
performance
is the number zero? The empty string?if (performance !== undefined) . . .
true
or false
in conditions.&& and || or ! not
if (i < a.length && a[i] > 0) //a[i] > 0
is not evaluated ifi
≥a.length
0 && 'Harry' // 0 0 || 'Harry' // 'Harry'
const result = arg && arg.someMethod() const result = arg.someMethod() || 0
const max = x > y ? x : y
& | ^ ~
truncate operands to 32-bit integers.<< >> >>>
truncate left operand to 32-bit, right operand to 5-bit.x | 0 // Use Math.floor(x)
instead
if
Statementif (condition) statement
{ statement1 statement2 . . . }
else
clause:
if (yourSales >= target) { performance = 'Satisfactory' bonus = 100 } else { performance = 'Unsatisfactory' bonus = 0 }
const description = '' switch (value) { case 0: description = 'zero' break case false: case true: description = 'boolean' break case '': description = 'empty string' // Error—fall through! default: description = 'something else' }
if (parseFloat(input) === NaN) { Invalid input... }
// What if the user entered NaN
?
JSON.parse
throws an exception if it can't parse the input.try
block:
try { const input = ... const data = JSON.parse(input) // Process data... } catch { // Invalid input... }
while
and do/while
Loopswhile (i < s.length) { if (s[i] == ' ') count++ i++ }
do { i++ } while (i < s.length && s[i] != ' ')
for
Loopfor (let i = 1; i <= 10; i++) console.log(i)
for (let i = 0, j = a.length - 1; i < j; i++, j--) { const temp = a[i] a[i] = a[j] a[j] = temp }
for of
Loopconst arr = [1, 2, , 4] arr[5] = 100 for (const element of arr) // Visits 1, 2,undefined
, 4,undefined
, 100 console.log(element)
const greeting = 'Hello 🌐' for (const c of greeting) // VisitsH e l l o
, a space, and🌐
console.log(c)
for in
Loopconst obj = { name: 'Harry Smith', age: 42 } for (const key in obj) // Visits'name'
,'age'
console.log(key)
const arr = [1, 2, , 4] arr[5] = 100 for (const key in arr) // Visits'0'
,'1'
,'3'
,'5'
console.log(key)
const greeting = 'Hello 🌐' for (i in greeting) // Visits both code units of 🌐 separately console.log(greeting[i])
let i = 0
while (i < arr.length) {
if (arr[i] === 0) break
i++
}
let i = 0 let found = false while (!found && i < arr.length) { if (arr[i] === 0) found = true else i++ }
let i = 0 outer: while (i < arr.length) { let j = 0 while (j < arr[i].length) { if (a[i][j] === 0) break outer j++ } i++ }
let count = 0 let sum = 0 for (let i = 0; i < arr.length; i++) { if (arr[i] === 0) continue count++ sum += arr[i] } const avg = count === 0 ? 0 : sum / count
continue