false
'l'
'cat'
10
{lat: 33, lon: -112}
0.30000000000000004
false
1.111111111111111e+29
111111111111111111111111111111n
["I", "do", "not", "think"]
{dog: "rex", c: ["Black", "Purple", "Red", "Green", "Blue", "Cyan"]}
'["home",{"lat":33,"lon":-112}]'
"Not today"
100
-2
((2 * 5) - (7 / (- 6))) + 4
(2 > 4) || (true && false)
(1 <= 2) <= 3
((! x) || ((! y) && z)) || ((6 / 5) & 1)
(- (- 9)) - (--x)
s.substring(s.length-5)
ors.slice(-5)
x.toString(2)
a.splice(8, 7)
a.splice(0, 0, 0, 0, 0)
ora.unshift(0, 0, 0)
sick || weekend || holiday
Math.max(...a)
Math.floor(4 * x)
Math.floor(Math.random() * 16 + 5)
'na'.repeat(16)
s.includes('💩') || s.includes('😉')
Math.hypot(8, 3, 99, 14, -3)
t[4][2].x
new Date(0).toLocaleString('ar-EG')
ornew Date(Date.UTC(1970, 0, 1)).toLocaleString('ar-EG')
{
A:2, B:2, C:2, D:3, E:3, F:3, G:4, H:4, I:4,
J:5, K:5, L:5, M:6, N:6, O:6, P:7, Q:7, R:7, S:7,
T:8, U:8, V:8, W:9, X:9, Y:9, Z:9
}
Number.isInteger(x) && x > 0 && x < 1E6 * Math.PI
const notice = "She is" + present ? "" : "n't" + " here."assigns the value
""
to notice no matter what value is stored in the variable present
. The reason has to do with precedence. The operator ?:
has lower precedence than +
, therefore the above statement is identical to:
const notice = ("She is" + present) ? ("") : ("n't" + " here.")Regardless of the value of
present
, the leftmost operand of the conditional expression will be a non-empty string (for example "She istrue"
or "She isfalse"
), which is truthy. Therefore the value of the expression will be the middle argument of the conditional, which is (always) the empty string.
The preferred way to write this code is:
const notice = `She is ${present ? "" : "n't"} here.`
Here there are no precedence issues, since we never see two operators at the same syntactic level.
const WIDTH_IN_LIGHT_YEARS = 93e9 const SPEED_OF_LIGHT = 299792458 const DAYS_PER_YEAR = 365.2422 const SECONDS_PER_DAY = 60 * 60 * 24 const METERS_PER_YOTTAMETER = 1e24 const WIDTH_IN_YOTTAMETERS = WIDTH_IN_LIGHT_YEARS * DAYS_PER_YEAR * SECONDS_PER_DAY * SPEED_OF_LIGHT / METERS_PER_YOTTAMETER console.log(`The universe is ${WIDTH_IN_YOTTAMETERS} Ym wide.`)
const employee = { name: 'María', salary: { period: 'week', amount: 1988, currency: 'USD' }, hired: new Date(2008, 0, 5), supervisor: null } const dayNames = [ 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo' ] const song = { title: 'Johnny Tarr', artist: 'Gaelic Storm', trackNumber: 3, album: 'Tree', // IRL this would be a separate object releaseDate: new Date(2001, 5, 19), producer: 'Jim Cregan', writers: ['Steve Wehmeyer', 'Steve Twigger', 'Jim Cregan', 'Shep Lonsdale'], genre: 'Celtic', lengthInSeconds: 210, ASIN: 'B003LLOIR6', label: 'Higher Octave', openingLyrics: 'Let me tell you a little story about a man named Johnny Tarr', synopsis: 'Hard drinker dies of thirst' }
const artist = { name: 'Purity Ring', origin: { city: 'Edmonton', province: 'Alberta', country: 'CA' }, members: [ {name: 'Megan James', role: 'vocalist'}, {name: 'Clayton Knight', role: 'producer'} ], genres: ['synth-pop', 'trip hop', 'dream pop', 'witch house'], website: 'purityringthing.com', studioAlbums: [ { title: 'Shrines', release: '2012-07-24', peaks: [{'ranking': 32, chart: 'Billboard 200'}] }, {title: 'Another Eternity', release: '2015-03-03'}, {title: 'Womb', release: '2020-04-03'}, ], labels: ['4AD', 'Last Gang'] }