JavaScript & TypeScript Flashcards

(48 cards)

1
Q

add an array to an existing array in JS

A

«array1».push(…«array2»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

deep copy a string to another const in JS

A

const «variable name» = (‘ ‘ + «string»).slice(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the primitive types in TS?

A
» undefined
» null
» any
» boolean
» number
» string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

declare a variable’s type in TS

A

«variable»: «type»

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what are the complex types in TS?

A
» void
» never
» Array
» object
» enum
» tuple
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

declare an enum in TS

A
enum Color {
  Red _= 1,
  Green _= 2,
  Blue _= 4
}

let c: Color = Color.Green
console.log( Color[2] )

» numbering starts at 0 as a default, set first number to change

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

declare an array in TS

A

let fruits: string[] = [‘apple’, ‘orange’, ‘banana’]

let fruits: Array = [‘apple’, ‘orange’, ‘banana’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

declare a tuple in TS

A

let coordinates: [number, number]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when to declare type void in TS?

A
function warnUser(): void {
  console.log('This is my warning message');
}

» for functions that don’t return a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

when to declare type never in TS?

A
function error(message: string): never {
  throw new Error(message);
}
» for functions that always throw errors
------------------------
function infiniteLoop(): never {
  while (true) {}
}

» for functions that never return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

cast variable as type in TS

A

«variable» as «type»

» only AS syntax is valid in JSX

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is an interface in TS?

A

» a name for a data structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

declare an interface in TS

A
interface «object interface name» {
  «prop1»: «type»
  «prop2»?: «type»   » optional
  readonly «prop3»: «type»   » readonly
}

» readonly value is set only in assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

declare a function interface in TS

A

interface «function name» {
(«param1»: «type», «param2»: «type»): «return type»
}

» parameter names do not need to match the implementation names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

declare an indexable object interface in TS

A

interface «object name» {
_readonly [«index name»: number | string]: «index value type»
«_another prop1»: «property type»
}

» property type must be subset of index value type
» readonly value is only set in assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

interpolate string in JS

A

I am ${«age»} years old

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

parse JSON string in JS

A

JSON.parse(«string»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

transform JSON object to string in JS

A

JSON.stringify(«json»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

difference between normal functions and arrow functions in JS

A

» arrow functions don’t bind their «this» value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

get first array element that matches a condition in JS

A

«array».find(«condition»)

» stops on finding the first matching element

21
Q

execute function with a delay in JS

A

setTimeout(«callback», «milliseconds»)

22
Q

set object property using object property shorthand in JS

A
const name = 'Edvard'
const years = '32'

const user = {
name
age: years
}

23
Q

destructure JS object property

A

const { «property» } = «object»

24
Q

destructure and name JS object property

A

const { «property»: «new name» } = «object»

25
destructure JS object property and set default value
const { «property» = «default value» } = «object»
26
destructure function parameter(s) from a JS object
``` function «name»({ «param 1», «_param 2» }) { ... } ```
27
extract keys of object into an array in JS
Object.keys(«object»)
28
check if an array contains a specific element in JS
«array».includes(«element»)
29
check if a string contains a specific substring in JS
«string».includes(«substring»)
30
check if every array element passes a specific test in JS
«array».every((«element») => «test result»)
31
replace part of string in JS
«string».replace(«to replace», «replacement») » replaces only first match
32
remove property from object in JS
delete «object».«property»
33
manipulate JSON.stringify() output of object in JS
``` «object».toJSON(() => { const value = this ... return «return value» }) ```
34
convert string to number in JS
parseInt(«string») | parseFloat(«string»)
35
naming convention for DOM elements in JS
$«name» » $button = document.querySelector('button')
36
disable DOM element in JS
«element».setAttribute('disabled', 'disabled')
37
enable DOM element in JS
«element».removeAttribute('disabled')
38
focus DOM element in JS
«element».focus()
39
remove leading an tailing whitspaces and newlines from string in JS
«string».trim()
40
remove element from array by its index in JS
«array».splice(«index», «count») » returns array of removed objects
41
get array index of element that complies a criteria in JS
«array».findIndex(«check function»)
42
combine array elements in JS
«array».reduce((accumulator, currentValue) => { ... return «value» }) » accumulator initiates as first array element » currentValue initiates as second array element
43
check if one or more array elements comply a criteria in JS
«array».some(«check function») » returns true or false
44
destructure array value(s) to variables in JS
const [«var 0», «_var 1»] = «array»
45
get array index of specific element in JS
«array».indexOf(«element»)
46
ignore specific TS error
``` //@ts-ignore «line of code with error» ```
47
round number in JS
Math.round(«number»)
48
format date to German date string in JS
«date».toLocaleDateString('de-DE')