JS Flashcards
ECMAScript
JavaScript standard
ES12
ECMAScript 2021
Babel
Transpiles JS code between versions
Node.js
JavaScript runtime environment based on Google’s Chrome V8 JavaScript engine
Run a JS file: node name_of_file.js
const vs let
const defined variables can’t be reassigned. If an array (object), the content of array can be changed
let defined variable can be reassigned a new value.
use const as a starting point.
array forEach
const t = [1, -1, 3]
t.push(5)
console. log(t.length) // 4 is printed
console. log(t[1]) // -1 is printed
t.forEach(value => {
console.log(value) // numbers 1, -1, 3, 5 are printed, each to own line
})
array in immutable way (functional)
const t = [1, -1, 3]
const t2 = t.concat(5) // creates an new array; does not change the original array
console. log(t) // [1, -1, 3] is printed
console. log(t2) // [1, -1, 3, 5] is printed
array map
const t = [1, 2, 3]
const m1 = t.map(value => value * 2) console.log(m1) // [2, 4, 6] is printed
destructuring assignment
const t = [1, 2, 3, 4, 5]
const [first, second, …rest] = t
console. log(first, second) // 1, 2 is printed
console. log(rest) // [3, 4, 5] is printed
object literal
One way of defining objects in JS listing its properties within braces.
const object1 = { name: 'Arto Hellas', age: 35, education: 'PhD', }
const object2 = { name: 'Full Stack web application development', level: 'intermediate studies', size: 5, }
const object3 = { name: { first: 'Dan', last: 'Abramov', }, grades: [2, 3, 5, 3], department: 'Stanford University', }
referencing object property
“dot” notation or brackets
console.log(object1.name) // Arto Hellas is printed const fieldName = 'age' console.log(object1[fieldName]) // 35 is printed
add properties to an object
object1.address = ‘Helsinki’
object1[‘secret number’] = 12341
The latter of the additions has to be done by using brackets, because when using dot notation, secret number is not a valid property name because of the space character.
Arrow function (complete)
(p1, p2) => { console.log(p1) console.log(p2) return p1 + p2 }
// assign the function to a variable const sum = (p1, p2) => { console.log(p1) console.log(p2) return p1 + p2 }
Arrow function (single parameter)
// exclude the parentheses const square = p => { console.log(p) return p * p }
Arrow function (single expression)
// exclude braces const square = p => p * p // commonly used in map const t = [1, 2, 3] const tSquared = t.map(p => p * p)