Last active
November 30, 2023 19:53
-
-
Save GeorgeHernandez/4819ba17676da1e14b31073a05c587fe to your computer and use it in GitHub Desktop.
Quick notes on basic JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @fileoverview Quick notes on basic JavaScript stuff | |
* Via: https://gist.github.com/GeorgeHernandez/4819ba17676da1e14b31073a05c587fe | |
* FYI: This document assumes Semicolon Insertion (ASI) | |
*/ | |
/** | |
* Declaration const let var | |
* Safety High Medium Low | |
* Scope Block Block Global or Function | |
* Initialized MUST MAY MAY | |
* Reassignable MUST NOT MAY MAY | |
* Redeclarable MUST NOT MUST NOT SHOULD NOT | |
* Hoistable MUST NOT MUST NOT MAY | |
*/ | |
const cName1 = value1 [, cName2 = value2 [, ... [, cNameN = valueN]]] | |
let lName1 [= value1] [, lName2 [= value2] ... [, lNameN [= valueN]]] | |
var vName1 [= value1] [, vName2 [= value2] ... [, vNameN [= valueN]]] | |
// JSDoc | |
/** | |
* Description of myFunction in Markdown | |
* @param {(string|Array)} p1 - Description of p1 | |
* @param {boolean} p2 - Description of p2 | |
* @param {a: Date, b: number[]} p3 - Description of p3 | |
* @return {number} - Description of what's returned | |
* @author George Hernandez <georgelhernandez@gmail.com> | |
*/ | |
var myFunction = function (p1, p2, p3) { | |
/** @type {string} */ | |
let x = 'foo' | |
/** @todo Do something here */ | |
} | |
// If...else | |
var foo, bar | |
if (foo < 10) { | |
bar = 'a' | |
} else if ((foo >= 10) && (foo < 20)) { | |
bar = 'b' | |
} else { | |
bar = 'c' | |
} | |
// if...else as conditional ternary | |
let status = (age >= 18) ? 'adult' : 'minor' | |
// Switch does strict equality (===) on cases | |
switch (day) { | |
case 1: | |
x = 'MOANday' | |
break | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
x = 'WEAKday' | |
break | |
case 0: | |
case 6: | |
x = 'ENDday' | |
break | |
default: | |
x = 'WHATday' | |
} | |
// For | |
const x = [1, 2, 4] | |
for (let i = 0; i < x.length; i++) { | |
console.log('i: ', i, ', x[i]:', x[i]) | |
} | |
/* For...in | |
Loop through enumerable properties of an object. | |
Mnemonic: "Foreign keys". | |
*/ | |
const o = {a:1, b:2, c:3} | |
for (const property in o) { | |
console.log(`${property}: ${o[property]}`) | |
} | |
/* For...of | |
Loop through items of an iterator. | |
Mnemonic: "Four of them" | |
*/ | |
for (const c of 'cat') { console.log(c.toString()) } // 'c', 'a', 't' | |
/* Do...while | |
Executes at least once. | |
*/ | |
let result = '', i = 0 | |
do { | |
i = i + 1 | |
result = result + i | |
} while (i < 5) | |
console.log(result) // 12345 | |
// While | |
const a2 = ['a', 'b', 'c'] | |
let i = 0 | |
while (i < a.length) { | |
console.log(a2[i]) | |
i++ | |
} | |
// Date. Stored as ms since 1970-01-01 00:00:00 UTC | |
new Date() // create a new Date instance representing the current time/date. | |
new Date(2010, 2, 1) // 2010-Mar-01 00:00:00 | |
new Date(2010, 2, 1, 14, 25, 30, 2) // 2010-Mar-01 14:25:30.002 | |
new Date('2010-3-1 14:25:30') // create a new Date instance from a String. | |
// String. An ordered sequence of immutable characters. | |
let a = 'Jon "Johnny" O\'Malley' | |
let b = "Jon \"Johnny\" O'Malley" | |
let c = `Jon "Johnny O'Malley` | |
let d = `String templates can be multi-line & do string interpolations | |
like ${a + b} etc. Don't "stab" me.` // Since ES6 (2015) | |
'cat'.length // 3 | |
'cat'.charAt(2) // 't'. 'cat'[2]; 'cat'.at(2); 'cat'.at(-1) | |
'cat'.substring(1, 3) // 'at'. 'cat'.slice(1); 'cat'.slice(-2) | |
'b' < 'c' // true | |
'b' < 'B' // true. Use .toLowerCase() to ignore case | |
const s = "xxxyz" | |
s.search('y') // 3 | |
s.replace('x', 11) // 'xxyz' | |
s.replace(/x/g, "") // 'yz'. s.replaceAll('x', '') | |
s[1] = 'a' // Fails because JS strings are immutable. | |
// Array. An ordered set of mutable items, each of any type, with a 0 index | |
var a = [] // An array literal | |
var al = a.length // 0. Same as a['length'] | |
a[0] = 1 // [1] | |
a[1] = 2 // [1, 2] | |
a[1] // 2. a.at(1); a.at(-1) | |
a.join('<br') // By default joins with ',' | |
// LIFO, stack, heap | |
a.push('x') // 3, i.e. [1, 2, 'x']. Adds item at end & returns length | |
a.pop() // 'x', i.e. [1, 2]. Removes last item & returns it | |
// FIFO, queue | |
a.unshift('a') // 3, i.e. ['a', 1, 2]. Adds item at start & returns length | |
a.shift() // 'a', i.e. [1, 2]. Removes first item & returns it | |
for (let i = 0; i < al; i++) { | |
console.log('i: ', i, ', x[i]:', x[i]) | |
} | |
// Set. A collection of DISTINCT values of any type in the order inserted. | |
var set = new Set() | |
set.add(1) | |
set.add('foo') | |
set.add('foo') // Already present, so no effect | |
set.add('bar') | |
set.size // 3 | |
set.has(1) // true | |
for (let item of set) console.log(item) // 1, 'foo', 'bar' | |
set.delete('foo') | |
set.clear() // Removes all elements | |
// Map. A collection of key-value pairs in the order inserted. | |
// Any type may be used for key or value but the keys are distinct. | |
let kvArray = [['key1', 'value1'], [2, {}]] | |
let map = new Map(kvArray) | |
map.set(NaN, 'value3') | |
map.set(2, true) // Key already added, so update value | |
map.size // 3 | |
map.get('key1') // value1 | |
map.has('key1') // true | |
for (const [key, value] of myMap) { | |
console.log(key + ' = ' + value) | |
} | |
let myArray = [...map] // Convert map to an array | |
map.delete('key1') // true if removed | |
map.clear() // Removes all elements |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment