Javascript Flashcards

1
Q

What is the purpose of variables?

A

to store information to be referenced/accessed later

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

How do you declare a variable?

A

use the keyword “var”

  • var newVariable
  • (also use const/let)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you initialize (assign a value to) a variable?

A

put an equals sign after the variable you want to initialize, and then put the value after the equals sign
e.g. newVariable = ‘new value’

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

What characters are allowed in variable names?

A
  • variable must begin with: letter, $, or _. after that, numbers can be used as well
  • cannot use keywords
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does it mean to say that variable names are “case sensitive”?

A

capital and lower casing matter

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

What is the purpose of a string?

A

stores a sequence of characters (text)

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

What is the purpose of a number?

A

gives us a numeric value as a constant to work with

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

What is the purpose of a boolean?

A

indicates two possible values (true/false)

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

What does the = operator mean in JavaScript?

A

assigns a value to the variable that’s on the left of the operator

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

How do you update the value of a variable?

A

varName = ‘new updated value’

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

What is the difference between null and undefined?

A
  • null is an object with the assigned value of “no value”
  • undefined is a type, where the variable is undeclared or undefined (not been given a value)
  • in your code, you should not use “undefined”. undefined is generally used for debugging
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it a good habit to include “labels” when you log values to the browser console?

A

so that you know the context for the printed value

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

Give five examples of JavaScript primitives.

A

string, number, boolean, undefined, null

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

What data type is returned by an arithmetic operation?

A

number

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

What is string concatenation?

A

combination of 2+ values

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

What purpose(s) does the + plus operator serve in JavaScript?

A

adding number values or concatenation

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

What does the += “plus-equals” operator do?

A

adds the right-hand value onto the left-hand value and assigns it to the left-hand value
—> (x += 5) is the same as (x = x+5)

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

What are objects used for?

A

used to hold a collection of properties and methods

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

What are object properties?

A

a name-value pair that tells us more about the object

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

Describe object literal notation

A
objectName = {
	name: value,
	name2: value2,
	name3: value3
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you remove a property from an object?

A

delete objectName.propertyName

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

What are the two ways to get or update the value of a property?

A

dot notation, bracket notation

  • dot notation is very literal. property identifiers cannot be a variable, or start w a number, or have spaces, etc
  • bracket notation has fewer limitations. property identifiers can be variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are arrays used for?

A

arrays store a collection of elements of the same data type

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

Describe array literal notation.

A

var newArray = []

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

How are arrays different from “plain” objects?

A
  • plain objects have properties
  • arrays store lists of data of the same type
  • arrays have a numeric key [a, b, c] is {0: a, 1: b, 2: c}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What number represents the first index of an array?

A

0

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

What is the length property of an array?

A

tells you how many elements are in the array

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

How do you calculate the last index of an array?

A

array.length-1

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

What is a function in JavaScript?

A

a block of code that does something or calculates something

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

Describe the parts of a function definition.

A
  • the function keyword
  • an optional name
  • zero or more parameters
  • a code block
  • an optional return statement
function newFunction(parameter1, parameter2) {
xyz action
return xyz
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Describe the parts of a function call.

A

functionName(arguments)

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

When comparing them side-by-side, what are the differences between a function call and a function definition?

A
  • function definition states what action the function will perform w its arguments
  • function call invokes the function to perform the action
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is the difference between a parameter and an argument?

A
  • parameter is a placeholder for where arguments will be applied
  • argument is the actual value that will be going into the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Why are function parameters useful?

A
  • states all the information that will be needed to run the function
  • allows you to reuse code with different data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What two effects does a return statement have on the behavior of a function?

A
  • causes the function to produce a value

- exits the function

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

Why do we log things to the console?

A

console logging helps developers keep track of their code, making sure the outputs are correct

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

What is a method?

A

a function stored as a property of an object

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

How is a method different from any other function?

A
  • a method acts on the data of the object it belongs to

- a function is standalone

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

How do you remove the last element from an array?

A

arrayName.pop()

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

How do you round a number down to the nearest integer?

A

Math.floor(number)

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

How do you generate a random number?

A

Math.random()

  • multiply it by a number you want the range of
  • the formula==>Math.floor(Math.random() * (end - start) +1) + start
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

How do you delete an element from an array?

A

arrayName.splice(start index, # of elements to be deleted, item1 to be added, item2, …)
-only the start index is required as an argument

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

How do you append an element to an array?

A

arrayName.push(newElement)

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

How do you break a string up into an array?

A

arrayName.split(‘’)

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

Do string methods change the original string? How would you check if you weren’t sure?

A
  • strings are immutable
  • string methods do not change the original string (they create new outputs)
  • you can check by console logging the string value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Roughly how many string methods are there according to the MDN Web docs?

A

30-ish

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

Is the return value of a function or method useful in every situation?

A

no, sometimes the function or method is used just to perform an action

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

Roughly how many array methods are there according to the MDN Web docs?

A

36

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

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

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

How do you pre-pend an element to an array?

A

arrayName.unshift()

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

Give 6 examples of comparison operators.

A

==, !=, >, =, <=

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

What data type do comparison expressions evaluate to?

A

boolean

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

What is the purpose of an if statement?

A

sets a condition to help the computer decide which lines of code should be run next

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

Is ‘else’ required in order to use an if statement?

A

no

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

Describe the syntax (structure) of an if statement.

A

if (condition) {
code to be run if condition is met
}

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

What are the three logical operators?

A

and &&
or ||
not !

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

How do you compare two different expressions in the same condition?

A

(expression1) &&/|| (expression2)

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

What is the purpose of a loop?

A

-run the same block of code a certain amount of time

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

What is the purpose of a condition expression in a loop?

A

lets the computer know when to stop the loop

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

What does “iteration” mean in the context of loops?

A

represents each instance of the code block in the curly brackets of the loop being run

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

When does the condition expression of a while loop get evaluated?

A

after every iteration

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

When does the initialization expression of a for loop get evaluated?

A

once, at the beginning

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

When does the condition expression of a for loop get evaluated?

A

the condition gets checked at the beginning of every iteration, following initialization

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

When does the final expression of a for loop get evaluated?

A

after the code block is run

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

Besides a return statement (which exits its entire function block) which keyword exits a loop before its condition expression evaluates to false?

A

break

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

What does the ++ increment operator do?

A

adds +1 to the variable

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

How do you iterate through the keys of an object?

A

“for in” loop

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

when should you use a for loop vs a while loop vs a do while loop?

A
  • for loop: need to run the code a specific number of times (condition is a counter)
  • while loop: need to run the code an unknown number of times (condition is not a counter)
  • do while loop: similar to a ‘while loop’ but will always run the statements inside curly brackets at least once, even if the condition is false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

Why do we log things to the console?

A

for debugging purposes so that we know what our output is

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

What is a “model”?

A
  • a representation of the actual thing

- in the DOM, a model is the data representation of all the objects that make up a web document

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

Which “document” is being referred to in the phrase Document Object Model?

A

-web document: the html page

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

What is the word “object” referring to in the phrase Document Object Model?

A

-javascript object

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

What is a DOM Tree?

A

the data representation of all the objects that make up a web document where there is a parent stem that branches into child branches

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

Give two examples of document methods that retrieve a single element from the DOM.

A
  • getElementById(‘id-name’)
  • querySelector(‘css selector’)

*the best one to use is querySelector

75
Q

Give one example of a document method that retrieves multiple elements from the DOM at once.

A
  • getElementsByClassName(‘class’)
  • getElementsByTagName(‘tagName’)
  • querySelectorAll(‘css selector’)

*the best one to use is querySelectorAll

76
Q

Why might you want to assign the return value of a DOM query to a variable?

A

so you can re-use that reference in your javascript code without js having to search thru the entire html document

77
Q

What console method allows you to inspect the properties of a DOM element object?

A

console.dir(‘element’)

78
Q

Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A

the browser needs to finish loading all the elements in the html page before js can access them

79
Q

What does document.querySelector() take as its argument and what does it return?

A

it takes in a css selector and returns the first element in the dom that matches the selector

80
Q

What does document.querySelectorAll() take as its argument and what does it return?

A

it takes in a css selector and returns all the elements in the dom that match the selector

81
Q

What is the purpose of events and event handling?

A

allows us to execute code only when a certain event is happening

The handler is simply a function which does something (it’s executed) when the event happens. The handler function, by default, when executed is passed the event object (that was created when the event/action you are interested in happened) as an argument.

82
Q

Are all possible parameters required to use a JavaScript method or function?

A

-no
-ie. eventTarget.addEventListener takes in up to 4 parameters, but you really only use the first two
eventTarget.addEventListener(type, listener, useCapture, wantsUntrusted)

83
Q

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

.addEventListener(‘click’, handleClick)

‘click’ is the action to listen for
handleClick is your function

84
Q

What is a callback function?

A

a function passed into another function as an argument to be executed later

85
Q

What object is passed into an event listener callback when the event fires?

A

event object (this will be generated when the event fires. will generate properties such as “event.target”, “event.altKey”, etc)

86
Q

What is the event.target?
If you weren’t sure, how would you check?
Where could you get more information about it?

A
  • event.target is where the element on which the event happened originated from (its reference)
  • you can check by console logging “event.target”
  • or look it up on MDN
87
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A
  • the variable handleClick is held as a callback function (browser will call the function)
  • the function handleClick() is called immediately (you are the one calling the function)
88
Q

What is the difference between event handler and event listener?

A
  • event handler: defines what to do when the function is called (when the event occurs)
  • event listener: uses the event handler (callback function) to do something when the event occurs
89
Q

What is the className property of element objects?

A

sets/updates/returns the class name of an element

90
Q

How do you update the CSS class attribute of an element using JavaScript?

A

$button.className = ‘newClassName’

91
Q

What is the textContent property of element objects?

A

represents the text content of the node and also the text content of its descendents
(differs from “innerText” which only represents “human-readable” elments)

92
Q

How do you update the text within an element using JavaScript?

A

$button.textContent = ‘new text content’

93
Q

Is the event parameter of an event listener callback always useful?

A

no, it’s useful when the handler function needs to know about the event that happened

94
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

if the information is only stored within the DOM, js would have to search other places (like the entire html document) to find it

95
Q

Does the document.createElement() method insert a new element into the page?

A

no, it’s stored in a variable and not added to DOM until you use appendChild()

96
Q

How do you add an element as a child to another element?

A

appendChild()

97
Q

What do you pass as the arguments to the element.setAttribute() method?

A

element. setAttribute(name, value)
- name: a DOMString specifying the name of the attribute
- value: a DOMString containing the attribute’s value

98
Q

What steps do you need to take in order to insert a new element into the page?

A
  1. create the element
    - createElement()
  2. give it content (optional)
    - createTextNode()
  3. add it to the DOM
    - appendChild()
99
Q

What is the textContent property of an element object for?

A

return or change the text content of the element

100
Q

Name two ways to set the class attribute of a DOM element.

A
  • element.className = newClassName

- element.setAttribute(‘class’, ‘value’)

101
Q

What are two advantages of defining a function to do or create something (like the work of creating a DOM tree)?

A
  • you can reuse the function
  • makes the program easier to read because you can organize it into smaller chunks
  • need the DOM tree in multiple sections
102
Q

What property of form a control object gets and sets its value?

A

value

103
Q

What property of a form element object contains all of the form’s controls.

A

elements

104
Q

What does submitting a form without event.preventDefault() do?

A

prevents the form from submitting

105
Q

What does the event.preventDefault() method do?

A

prevents the default event from happening (checkboxes go unchecked, form submissions do not refresh the page, etc)

106
Q

What event is fired when a user clicks the “submit” button within a ?

A

submit

107
Q

What event is fired as a user changes the value of a form control?

A

input

108
Q

What event is fired when a user’s cursor leaves a form control?

A

blur

109
Q

What event is fired when a user places their cursor in a form control?

A

focus

110
Q

What is the event.target?

A

where the event occurred (the target object)

111
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A

event is carried forward to each level of its anscestral chain

112
Q

What DOM element property tells you what type of element it is?

A

event.target.tagName

nodeName could also work

113
Q

What does the element.closest() method take as its argument and what does it return?

A

targetElement.closest(selectors)
-ex. $button.closest(‘.div-class’)

it returns the DOM element that is closest to the chain that matches the selector

114
Q

How can you remove an element from the DOM?

A

element.remove();

115
Q

If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?

A

add it to the closest parent element that contains all the elements (event delegation)

116
Q

What is the effect of setting an element to display: none?

A

removes it from the document flow and causes the element and all of its children to be invisible

117
Q

What does the element.matches() method take as an argument and what does it return?

A
  • takes a selector as its argument and returns a boolean

- ex. element.matches(‘selector’)

118
Q

How can you retrieve the value of an element’s attribute?

A

element.getAttribute(‘attribute-name-such-as-class-or-src-etc’)

119
Q

If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?

A

you would have to put event listeners on every single tab

120
Q

If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?

A

you would have to have functions for every scenario (since there’s 3 tabs, you would need 3 functions)

121
Q

What is JSON?

A

a text-based data format following JavaScript object syntax

122
Q

What are serialization and deserialization?

A
  • serialization: converts object into bytes

- deserialization: converts bytes into object

123
Q

Why are serialization and deserialization useful?

A

makes it possible to transfer data across different platforms

124
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

JSON.stringify()

125
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

JSON.parse()

126
Q

How do you store data in localStorage?

A

storage.setItem(keyName, keyValue)

127
Q

How do you retrieve data from localStorage?

A

storage.getItem(keyName)

128
Q

What data type can localStorage save in the browser?

A

strings

129
Q

When does the ‘beforeunload’ event fire on the window object?

A

before the browser discards the data

130
Q

How can you tell the difference between a method definition and a method call?

A
  • method definition is written inside an object and describes what the method will do with the object
  • method call carries out the method definition
131
Q

Describe method definition syntax (structure).

A
var newObj = {
  add: function (x, y) {
    return x + y;
  }
}
132
Q
Describe method call syntax (structure).
for the following:
var newObj = {
  add: function (x, y) {
    return x + y;
  }
}
A

newObj.add(3, 2)

133
Q

How is a method different from any other function?

A

-a method is associated with an object

134
Q

What is the defining characteristic of Object-Oriented Programming?

A

-software is designed using objects to stand for data (as properties) and behavior (as methods)

135
Q

What are the four “principles” of Object-Oriented Programming?

A

-abstraction, encapsulation, inheritance, polymorphism

136
Q

What is “abstraction”?

A

-being able to work with complex things in simple ways, e.g. flipping a light switch, using automatic transmission

137
Q

What does API stand for?

A

application programming interface

138
Q

What is the purpose of an API?

A

-allows applications to access and interact with external data

139
Q

What is ‘this’ in JavaScript?

A

“this” is an implicit parameter that refers to the object that “this” was located in when it was called

140
Q

What does it mean to say that “this” is an “implicit parameter”?

A

“this” is an implicit parameter because it’s available in a function’s code block even though it was never included in the function’s parameter list or declared with var

141
Q

When is the value of “this” determined in a function; call time or definition time?

A

call time!!

142
Q
What does "this" refer to in the following code snippet?
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
A

nothing! (not even window)

“this” is present but not yet called

143
Q
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};

What is the result of the following code snippets, and why?
character.greet();

~~and~~

var hello = character.greet();
hello();
A

character.greet() will return
It’s-a-me, Mario!

in this code block, “this” is referring to var “character”, which has the firstName property of ‘Mario’.

hello() will return
It’s-a-me, undefined!

here, the greet function is copied from the character object onto “hello”, so is now a free-standing function. when hello is called, “this” is referring to window which does not have a firstName property

144
Q

How can you tell what the value of “this” will be for a particular function or method definition?

A

you can’t know the value of “this” until you can see where the function or method is called

145
Q

How can you tell what the value of “this” is for a particular function or method call?

A

Find where the function is called and look for an object to the left of the dot.
if in a general function, the value of “this” is window

146
Q

What kind of inheritance does the JavaScript programming language use?

A

prototypal chain

147
Q

What is a prototype in JavaScript?

A

a prototype is a reference to another object that contains common attributes/properties across all instances of the object

148
Q

How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?

A

prototype object is a built-in property

149
Q

If an object does not have its own property or method by a given key, where does JavaScript look for it?

A

in the object’s prototype (Object.prototype)

150
Q

What does the new operator do?

A
  1. creates a new js object
  2. adds a property to the new object (__proto__) that links to the constructor function’s prototype object
  3. binds the newly created object as the “this” context
151
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

prototype

152
Q

What does the instanceof operator do?

A
  • tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object
  • returns a boolean
153
Q

how do you get a __proto__ property?

A

it is inherited from the Prototype property of a constructor as a new object from that constructor

154
Q

What is a “callback” function?

A

-a function passed into another function as an argument or value

155
Q

Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?

A

-setTimeout()

156
Q

How can you set up a function to be called repeatedly without using a loop?

A
  • setInterval() method

- repeatedly calls a function with a fixed time delay between each call

157
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0 (executed immediately)

158
Q

What do setTimeout() and setInterval() return?

A
  • setTimeout() returns “timeoutID”, which is a number that identifies the timer created by the call to setTimeout()
  • setInterval() returns “intervalID”, which is a number that identifies the timer created by the call to setInterval()
159
Q

What is a code block? What are some examples of a code block?

A
  • statements that are grouped together. in js, it’s whatever is in the curly brackets.
  • function() {}
  • for () {}
  • if () {}
160
Q

What does block scope mean?

A

-a variable assigned or declared within the code block can only be seen or accessed within that block

161
Q

What is the scope of a variable declared with const or let?

A

-block-scoped

162
Q

What is the difference between let and const?

A
  • “let” variables can be reassigned (updated but not re-declared)
  • “const” variables are read-only references (can neither be updated nor re-declared)
  • both are block-scoped, neither are global like “var” is
163
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A

-only the const variable’s reference must remain unchanged. but the Array itself is a separate entity, unbound to const, and can be changed as needed

164
Q

How should you decide on which type of declaration to use?

A
  • if you aren’t going to change the variable in the code block, then use “const”
  • if the variable will be changed, then use “var”
165
Q

What is the syntax for writing a template literal?

A

This is a template literal referring to a ${variable}

166
Q

What is “string interpolation”?

A

-the ability to substitute part of the string for variables or expressions

167
Q

What is destructuring, conceptually?

A

-allows you to pull properties from an object/array into a separate variable

168
Q

What is the syntax for Object destructuring?

A

let { firstName: fname, lastName: lname } = person;

-here, the properties from the variable “person” are pulled as variables “fname” and “lname”

169
Q

What is the syntax for Array destructuring?

A

let [x, y, z] = getScores();

-here, the first 3 array elements from getScores() are being assigned to variables x, y, and z

170
Q

How can you tell the difference between destructuring and creating Object/Array literals?

A

-in destructuring, the stuff to the left of the assignment operator is the array/object, and the stuff on the right of the assignment operator is a variable

171
Q

What is the syntax for defining an arrow function?

A
  • no parameters: need parentheses
  • 1 parameter: optional parentheses
  • 2+ parameters: need parentheses
172
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

-only an expression (one line of code is run)… return keyword is not needed

173
Q

How is the value of this determined within an arrow function?

A
  • based on where the arrow function is defined
  • look outside of the arrow function (the parent function), and if it’s another arrow function, then keep looking up and up
174
Q

What is the JavaScript Event Loop?

A

-the event loop is a model that is responsible for executing the code, collecting and processing events, and executing queued sub-tasks

175
Q

What is different between “blocking” and “non-blocking” with respect to how code is executed?

A
  • “blocking” means that no code is run until the current operation finishes
  • “non-blocking” means that the program runs asynchronously: it may not necessarily execute line by line. the program calls the function and moves onto the next operation without waiting for the first to return
176
Q

What are the three states a Promise can be in?

A
  • pending: initial state, neither fulfilled nor rejected.
  • fulfilled: meaning that the operation was completed successfully.
  • rejected: meaning that the operation failed.
177
Q

How do you handle the fulfillment of a Promise?

A

then()

178
Q

How do you handle the rejection of a Promise?

A

catch()

179
Q

What is Array.prototype.filter useful for?

A

creates a new array with all elements that pass a certain criteria

180
Q

What is Array.prototype.map useful for?

A

creates a new array populated with the results of calling a provided function on every element in the calling array

181
Q

What is Array.prototype.reduce useful for?

A

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

182
Q

What is the typeof an ES6 class?

A

function

183
Q

Describe ES6 class syntax.

A
class Person {
    constructor(name) {
        this.name = name;
    }
    getName() {
        return this.name;
    }
}