JavaScript & TypeScript Flashcards

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
Q

destructure JS object property and set default value

A

const { «property» = «default value» } = «object»

26
Q

destructure function parameter(s) from a JS object

A
function «name»({ «param1», «_param2» }) {
  ...
}
27
Q

extract keys of object into an array in JS

A

Object.keys(«object»)

28
Q

check if an array contains a specific element in JS

A

«array».includes(«element»)

29
Q

check if a string contains a specific substring in JS

A

«string».includes(«substring»)

30
Q

check if every array element passes a specific test in JS

A

«array».every((«element») => «test result»)

31
Q

replace part of string in JS

A

«string».replace(«to replace», «replacement»)

» replaces only first match

32
Q

remove property from object in JS

A

delete «object».«property»

33
Q

manipulate JSON.stringify() output of object in JS

A
«object».toJSON(() => {
  const value = this
  ...
  return «return value»
})
34
Q

convert string to number in JS

A

parseInt(«string»)

parseFloat(«string»)

35
Q

naming convention for DOM elements in JS

A

$«name»

» $button = document.querySelector(‘button’)

36
Q

disable DOM element in JS

A

«element».setAttribute(‘disabled’, ‘disabled’)

37
Q

enable DOM element in JS

A

«element».removeAttribute(‘disabled’)

38
Q

focus DOM element in JS

A

«element».focus()

39
Q

remove leading an tailing whitspaces and newlines from string in JS

A

«string».trim()

40
Q

remove element from array by its index in JS

A

«array».splice(«index», «count»)

» returns array of removed objects

41
Q

get array index of element that complies a criteria in JS

A

«array».findIndex(«check function»)

42
Q

combine array elements in JS

A

«array».reduce((accumulator, currentValue) => {

return «value»
})

» accumulator initiates as first array element
» currentValue initiates as second array element

43
Q

check if one or more array elements comply a criteria in JS

A

«array».some(«check function»)

» returns true or false

44
Q

destructure array value(s) to variables in JS

A

const [«var0», «_var1»] = «array»

45
Q

get array index of specific element in JS

A

«array».indexOf(«element»)

46
Q

ignore specific TS error

A
//@ts-ignore
«line of code with error»
47
Q

round number in JS

A

Math.round(«number»)

48
Q

format date to German date string in JS

A

«date».toLocaleDateString(‘de-DE’)