Basic Javascript Flashcards

1
Q

Basic class structure

A
class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

are classes hoisted?

A

no

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

create new instance of class

A

const square = new Rectangle(10, 10);

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

create an async function (eg to fetch something)

A
async function fName() {
  const fetchedData = await fetch('url here');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

default export

A

export default fName() {};

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

import default

A

import varName from “./fileName”;

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

named export

A

export const fName = () => {};

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

import named

A

import { fName } from “./fileName”

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

use Promise.all

A
return Promise.all([array, promises]).then((responses) => {
      let allDataObject = {};
      allDataObject["array"] = JSON.parse(responses[0]);
      allDataObject["promises"] = JSON.parse(responses[1]);
      return allDataObject
    });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does .then( … … … return x;) return?

A

it returns x wrapped in a promise

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

set an item in localstorage

A

window. localStorage.setItem(‘key’, ‘string value’)

note: value must be a string

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

get an item from localstorage

A

let val = window.localStorage.getItem(‘key’)

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

localStorage methods

A

localStorage.setItem(‘myCat’, ‘Tom’);
const cat = localStorage.getItem(‘myCat’);
localStorage.removeItem(‘myCat’);
localStorage.clear();

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

convert array/object to string

A

JSON.stringify(object)

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

convert from a string back to JSON object

A

JSON.parse(stringifiedObjectorArray);

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

difference between for( let i in x) and for(let i of x)

A

for ..in: iterates over enumeable properties of any object. Easiest way to loop over an OBJECT’s properties. Order not guaranteed.
Works for arrays and strings but not recommended because order not guaranteed.

for ..of: for..of is a ES2015 method for iterating over “iterable collections”.
Doesn’t work for objects.
Works well with ARRAYS and STRINGS. Also good for NodeLists

17
Q

use async and await

A
async function foo() {
   await bar();
}
18
Q

async await with try / catch

A

async function f() {

  try {
    let response = await fetch('/no-user-here');
    let user = await response.json();
  } catch(err) {
    // catches errors both in fetch and response.json
    alert(err);
  }
}

f();

19
Q

Object(dot) methods

A
Object.keys = returns array of keys 
Object.values = returns array of values
Object.entries = returns array of key-value tuples(arrays)
Object.fromEntries = makes an object from an array of key-value tuples(arrays)
20
Q

useful ES6 Array methods

A

array. forEach( (val) => { do something } )
array. map( (val) => { return something }) - returns an array of returned values
array. reduce ( (acc, val) => { return modified acc }, 0) - note 0 is acc starting value

array. filter( (val) => { return true to keep } ) - returns an array of kept values
array. includes(x) - returns true or false

21
Q

Does JS copy by value or by reference?

A

Simple data types copy by value
Arrays and Objects copy by reference

NB: copy array by value:
let arrB = […arrA]