Javascript Flashcards

1
Q

DOM

A

the browsers rendering of your HTML file (NOT the html file by itself)… we can manipulate it with javascript. when you refresh, all changes to DOM go away.

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

Variables

A

what you use to store data in memory when programming

the entity used to tell our program to remember a value for later.
stored in memory.
made up of two parts: declaration and assignment. think of it like a bucket in memory that is filled with data (declaration = bucket ; assignment = value/data stored in bucket)

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

Conditionals

A

used to make decisions using comparisons. value can be TRUE or FALSE.

if(age > 18){
//value is true so do this stuff
}
else{
//value is not true so do this stuff
}
Comparisons:
- equality -
9===9 is true
7===3 false
"Hello" ==== "Hello" true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Loops

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

Functions. How to create? How to call?

A

set of instructions

//declaration:
function name(parameters) {
//body
}
//calling the function:
name(arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Steps involved with variable creation

A

declaration: let age
assignment: age = 25

both at same time:
let age = 25

declaration is like creating a bucket for data
assignment is like putting something in the bucket.

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

what is a variable declaration? what does it do?

A

it creates a space in memory
example: let age

let is what allows it to create a new space in memory. it triggers the PC to be like “SOMETHING IS TRYING TO MAKE A DATA BUCKET”

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

what is variable assignment?

A

assigning a value to a declared space of memory.

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

types of data

A

numbers, strings, etc

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

What is a String

A

stores text; always surrounded by double quotes(“), single quotes(‘), or ticks(`)

may need to use an escape character depending on order…. using \

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

What are some data types within that are Numbers? How can you manipulate them?

A

int - whole numbers
float - decimal points
note: can be signed (positive or negative)

manipulation: + - * / % (modulus - the remainder)

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

what can modulus be used for to figure out?

A

figuring out if a number is even or odd (x%2==0 means even). figuring out if a number is divisible by another number.

fizzbuzz programming challenge:
if divisible by 3 print fizz, divisible by 5 print buzz, divisible by 3 and 5 print fizzbuzz

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

what happens to user interactions involving javascript when the page is refreshed?

A

everything resets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
difference between 
=
==
===
!=
!==
A

= is assignment
== is comparing if values are same
=== is comparing if values and data types are the same
!= checks if values are NOT the same
!== checks if values or data types are NOT the same

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

how do you add Leon’s smurf to an element, waiting for a click to happen?

A

document.querySelector(“#elementID”).addEventListener(‘click’, myFunction)

function myFunction() {
//do what you want with the click here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

change an element’s(id=”dog”) text content to “chihuahua”

A

document.querySelector(“#dog”).innerText = “chihuahua”

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

what are constants? what are best practices for naming them?

A

constants are variables that can only be assigned once.

if you know the value by run time, like 
const COLOR_ORANGE = "#FF7F00";

then its recommended to put name in all caps.

if they are assigned during run time, just do it normally. like
const pageLoadTime = /* */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

argument vs parameter

A

parameter = variable listed inside patentheses in function declaration (declaration time term)

atgument = value thats passed to function when it is called (call time term)

we declared functions listing their parameters, then call them passing arugments

example:
function showMessage(from, text) {
alert( from + ‘: ‘ + text );
}

showMessage(from, “Hello”);

In the example above, one might say: “the function showMessage is declared with two parameters, then called with two arguments: from and “Hello””.

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

how do you set a default parameter for a function?

A
function myFunction(message = "default message"){
}

or (message = anotherFunction()) - which will store the value that function returns by default

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

function naming best practices

A

since function are actions, they usually start with a verb… like
getUserName
createYoMama

21
Q

function functionality best practices

A

functions should be short and each do exactly one thing. if big, should probably be split in to smaller ones.

separate functions are easier to test and debug; its existsance is a great commment

22
Q

Rewrite the function using ‘?’ or ‘||’

function checkAge(age) {
  if (age > 18) {
    return true;
  } else {
    return confirm('Did parents allow you?');
  }
}
A

Using a question mark operator ‘?’:

function checkAge(age) {
  return (age > 18) ? true : confirm('Did parents allow you?');
}

Using OR || (the shortest variant):

function checkAge(age) {
  return (age > 18) || confirm('Did parents allow you?');
}

https://javascript.info/function-basics

23
Q

function declaration syntax

A
fucntion myFunction() {
//do stuff
}

can be called earlier than it’s defined. this is because javascript starts off by scanning for global functions.

24
Q

function expression syntax

A
let myFunction = function() {
//do stuff
};

(semi colon is recomended at end of a statement, which this is. not part of function declaration syntax though)

can only be called after it’s defined.

25
Q

in javascript, functions are ____

A

a value representing an “action”

if you say console.log(myFunction) it won’t run the function, it will print out the code (which is the value stored within the variable myFunction). console.log(myFunction()) would run the function code

26
Q

arrow function syntax

A

arrow functions are shorthand for function expressions:

let func = (arg1, arg2, ..., argN) => expression;
This creates a function func that accepts arguments arg1..argN, then evaluates the expression on the right side with their use and returns its result.

In other words, it’s the shorter version of:

let func = function(arg1, arg2, ..., argN) {
  return expression;
};
27
Q

when do arrow functions need an explicit “return”?

A

when they are multiline.

let doMath = (a,b) => {
let c = a/b;
return c*9;
};

console.log(doMath(1,3));

28
Q

literal vs structural array declaration

A

literal - let myArray = []

stuctural - Array myArray = new Array (need to double check syntax)

29
Q

array methods: how would you sort nums = [12, 5, 10] from lowest to highest

A

nums.sort((a,b)=>

30
Q

arrow functions can be used for…

A

implicant returns

31
Q

array method: how would you filter only evens from an array

A

array.filter(item=>item%2==0)

32
Q

declare an object literally.. a stopwatch and give it 4 properties and 2 methods

A

let stopwatch = {}

stopwatch. brand = “Nike”
stopwatch. size = “medium”
stopwatch. color = “gold”
stopwatch. shiny = “super shiney”
stopwatch. getBrand = ()=>console.log(stopwatch.brand)
stopwatch. changeColor(

33
Q

every single thing in javascript is …

A

an object

34
Q

object constructor vs object literal syntax

A
let user = new Object(); // "object constructor" syntax
let user = {};  // "object literal" syntax
35
Q

what do you call a variable attached to an object? what do you call a function attached to an object?

A
variable attached to object = property
function attached to object = method
36
Q

array.map method

A

nums = [2,5,7,2]

nums.map(n=>n*n)

37
Q

array.reduce method

A
puts results in new array.
let sum = nums.reduce ( (acc, c) => acc + c, 0 )

acc = accumulator; c = current value; 0 is where accumulator starts

38
Q

What is object oriented programming?

A

A style of programming the focuses on objects rather than procedural programming (functions). Procedural programming can get very disorganized, so object oriented programming was created to help with that. OOP groups up related variables and functions in to an object. Variables, when belonging to an object, are called properties. Functions, when belonging to an object, are called methods.

39
Q

reverseThisString = “backwards”

A

reverseThisString.split(‘’).reverse().join(‘’)

40
Q

how to reduce a string?

A

split, reverse, join

let str = "this is a sentence"
console.log(str.split('').reverse().join(''))
41
Q

What are the 4 pillars of object oriented programming?

A

Encapsulation - reduce complexity + increase reusability ;
grouping related variables and functions in to objects.

Abstraction - reduce complexity + isolate impact of change;
hide some properties and methods to make a simpler interface; reduced impact of strange

Inheritanece - eliminate redundant code;
allows you to remove redundant code. anything that’s shared can be defined in to a general object.

Polymorphism - refactor ugly switch/case statements
“many forms”; this allows you to get rid of long

42
Q

Factory Function vs Constructor Function

A

These both do the same thing: creates an object. This is a matter of preference. Most people coming from OOP languages prefer the Constructor Function, but should be familiar with both.

//Factory Function
function createCircle(radius) {
    return {
        radius,
        draw: function() {
            console.log("draw")
        }
    }
}

const circle = createCircle(1)

//Consutroctor Function
function Circle(radius) {
    console.log('this',this)
    this.radius = radiusthis.draw = function() {
        console.log('draw')
    }
}

const another = new Circle(1)

43
Q

array methods and what they do (map, filter, slice, splice)

A

map -> alters each element in an array, and spits out a new array.
filter -> filters out an array based on the criteria, and returns a new array.
slice -> creates a shallow copy of the array based on (starting index (inclusive), final index (exclusive)).
splice -> adds or removes a number of elements in an array, and mutates the original array.

44
Q

how does javascript work?

A

it is syncronous and single-threaded.
(we have to wait for every single thing to happen. wait for request to finish before yo ucan scroll, etc. but thats not what we see… why?

the environment you run JS in - browser. since it runs in browser yo ucan do things js couldnt do on its own.

45
Q

what is the main environment javascript runs in? and what does it allow it to do?

A

the browser.

javascript is singlethreaded and syncronous, but when it runs in the browser it has access to web apis (like THE DOM). requests are often made out to these web apis and THOSE tasks are run asyncronously… happening while the rest of the JS script continues to move on (in its mind it finished that “tasK” by passing it off)

46
Q

how did people used to handle async code in javascript (before promises)?

A
callback hell; the pyramid of doom. nesting functions in functions:
function houseOne(){
  setTimeout( () => {
      console.log('Papger 1 delivered")
      setTimeout( () => {
          console.log('Paper 2 delivered')
}, 2000)
}, 3000) }

houseOne()

47
Q

promise

A

revolutionized way we handle responses from async. left behind callbacks and moved toward chaining promises. that was also hard to read so then came out with async / await which has syntax more fmailiar to javascript.

48
Q

async/await

A

abstracts away promises

49
Q

In terms of the browser: what is the event loop? What is the call stack? What is the task/job queue? How are they connected?

A

execute line of code: gets put on call stack / if we make use of asyncronous things like web apis the returned info gets put in the queue … once the call stack is empty on the next iteration of event loop it grabs the first item in the queue