JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store values to use later on

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

How do you declare a variable?

A

var name

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

var name = “xxxx”

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

letters (uppercase, lowercase), $, underscore _ , numbers (can’t start with numbers)

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

lower case and upper case are not exchangeable in variable name

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

to store 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

to store numeric value

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

to store binary values and make decisions

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

assignment operator (to put value in something)

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

name = new value (do not need to redeclare var after it’s been declared first time)

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 assigned value (intentional). undefined = empty

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

to make it easier to debug

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, null, undefined,

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

adding strings together

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

add numerical values or concatenate strings

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

What data type is returned by comparing two values (, ===, etc)?

A

Boolean - true or false

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

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

A

add onto current variable value

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

What are objects used for?

A

to store relevant data together (grouping)

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

What are object properties?

A

to store additional data relevant to objects

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

Describe object literal notation.

A

{
property : value
}

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

How do you remove a property from an object?

A

delete operator

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

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

A

dot notation and bracket notation

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

What are arrays used for?

A

store multiple values

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

Describe array literal notation.

A

list of zero or more expressions, each of which represents an array element, enclosed in square brackets

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

How are arrays different from “plain” objects?

A

Arrays have an order & numeric indexes, and [ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
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
28
Q

What is the length property of an array?

A

true count of values stored in an array

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

How do you calculate the last index of an array?

A

length - 1

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

What is a function in JavaScript?

A

repeatable, reusable block of code with the potential to receive and return different values.

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

Describe the parts of a function definition.

A

keyword function, name of the function, parameter, opening curly brace

function functionName (parameter) {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function call.

A

name of the function followed by arguments

getFunction(2,2);

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

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

A
function definition has keyword and code block
function call has arguments and values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the difference between a parameter and an argument?

A

parameter is a placeholder

argument is data provided for the function call and returns a value

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

Why are function parameters useful?

A

they’re a placeholder

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

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

A

returns the value and

exits out of the function code block

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

Why do we log things to the console?

A

to display and debug

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

What is a method?

A

function that stores data about the object

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

How is a method different from any other function?

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

How do you remove the last element from an array?

A

“top” method - take off last item

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

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

A

floor method

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

How do you generate a random number?

A

random method

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

How do you delete an element from an array?

A

splice method

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

How do you append an element to an array?

A

push method

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

How do you break a string up into an array?

A

split method

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

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

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

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

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

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

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

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

A

30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
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
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

to determine whether to perform a function or not

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 block }

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

What are the three logical operators?

A

&&, ||, !

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

&& or || operator

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

Repeated block of code happening under some condition

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

tell the function when to stop running

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

one repetition of a loop code block

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

before each iteration (before it runs)

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

in the very beginning (first step)

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

after initialization and before the code block runs

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 each iteration

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 and assigns it to the var

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

Why do we log things to the console?

A

to test the output

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

What is a “model”?

A

representation (not the actual thing, but the recreation of it, usually in a smaller scale)

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

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

A

HTML document

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

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

A

Object = data type, object, in javascript

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

What is a DOM Tree?

A
73
Q

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

A

getElementById, querySelector

74
Q

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

A

querySelectorAll

75
Q

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

A

to be able to re-use it without having to call it over and over again

76
Q

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

A

dir (short for directory) method

77
Q

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

A

so that html can run before javascript does

78
Q

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

A

css selector, returns first element that matches

79
Q

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

A

css selector, and returns nodelist (houses list of DOM elements)

80
Q

What is the purpose of events and event handling?

A

perform an action when an event is called.

81
Q

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

A

no (e.g. third (boolean) value for addEventListener)

82
Q

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

A

addEventListener

83
Q

What is a callback function?

A

function definition being passed as an argument

84
Q

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

A

event object

85
Q

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

A

target is a property in the event object that tells you where the event originally occurred.

86
Q

What is the difference between these two snippets of code?

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

A

handleClick is being called as the event executes

handleClick() is being called (immediately)

87
Q

What is the className property of element objects?

A

get or set value of a className attribute

88
Q

What is the className property of element objects?

A
89
Q

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

A
90
Q

What is the textContent property of element objects?

A

To get or set the text content

91
Q

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

A
92
Q

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

A

no

93
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A
94
Q

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

A
95
Q

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

A

focus

96
Q

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

A

blur

97
Q

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

A

input

98
Q

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

A

submit

99
Q

What does the event.preventDefault() method do?

A

prevents default behavior from the event

100
Q

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

A

refresh the entire page

101
Q

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

A

elements property

102
Q

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

A

value property

103
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

hard to figure out where it went wrong

104
Q

What is an advantage of having your console open when writing a JavaScript program?

A

you can see what’s going on as you progress

105
Q

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

A

no

106
Q

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

A

appendChild method

107
Q

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

A

name and value

108
Q

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

A

createElement

parent.appendChild(child)

109
Q

What is the textContent property of an element object for?

A

assign new text to an element or see what the text is

110
Q

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

A

setAttribute

className

111
Q

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

A

you can re-use it and

give it a name

112
Q

What is the event.target?

A
113
Q

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

A
114
Q

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

A

tagName (element name in all caps)

115
Q

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

A

takes a selector as an argument, and returns the ancestor

116
Q

How can you remove an element from the DOM?

A

remove () method

117
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 ancestor element

118
Q

What is the event.target?

A

DOM element object representing the element where the event originally occured

119
Q

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

A

does not display/gets hidden

120
Q

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

A

takes CSS selector and returns boolean

121
Q

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

A

getAttribute method

122
Q

At what steps of the solution would it be helpful to log things to the console?

A

every step

123
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’d need to write a code for every single tab

124
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

separate conditional block for each view

125
Q

What is JSON?

A

Javascript Object Notion. data-interchange format

126
Q

What are serialization and deserialization?

A

Serialization is converting object into string aka bytes aka data
Deserialization is the opposite

127
Q

Why are serialization and deserialization useful?

A

seriliazation is good for storing a data

deserialization is good for using the data

128
Q

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

A

JSON.stringify

129
Q

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

A

JSON.parse

130
Q

How to you store data in localStorage?

A

setItem () method

131
Q

How to you retrieve data from localStorage?

A

getItem () method

132
Q

What data type can localStorage save in the browser?

A

string

133
Q

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

A

before the page unloads

134
Q

What is a method?

A

A method is a function which is a property of an object

135
Q

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

A

definition = property, assignment, function definition

method call = object.method()

136
Q

Describe method definition syntax (structure).

A

property: function {
code block
},

137
Q

Describe method call syntax (structure).

A

object.method(arguments)

138
Q

How is a method different from any other function?

A

methods needs to have name of the object and dot before it

139
Q

What is the defining characteristic of Object-Oriented Programming?

A

objects can contain both data (as properties) and behavior (as methods).

140
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

141
Q

What is “abstraction”?

A

simplifying complex process. modeling.

142
Q

What does API stand for?

A

application programming interface

143
Q

What is the purpose of an API?

A

connection between computers or between computer programs

144
Q

What is this in JavaScript?

A

this is an implicit parameter of all JavaScript functions

145
Q

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

A

parameter that we don’t need to specify, but is present

146
Q

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

A

when it is called

147
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. it’s not being called

148
Q

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

A

it’s-a-me Mario !

149
Q
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
A

it’s-a-me undefined !

150
Q

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

A

Nothing, since it’s not being called yet

151
Q

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

A

object to the left of the dot.

If no dot, then window

152
Q

What kind of inheritance does the JavaScript programming language use?

A

protoype

153
Q

What is a prototype in JavaScript?

A
154
Q

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

A
155
Q

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

A

prototype

156
Q

What does the new operator do?

A
  1. Creates a blank, plain JavaScript 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 instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
  4. Returns this if the function doesn’t return an object.
157
Q

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

A

prototype

158
Q

What does the instanceof operator do?

A

Checks if the prototype property of the right side of the operator is present in the prototype chain of the left side

159
Q

What is a “callback” function?

A

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

160
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

161
Q

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

A

setInterval

162
Q

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

A

delays it by 0 seconds (useful for having it occur after something else)

163
Q

What do setTimeout() and setInterval() return?

A

timeoutID which is a positive integer

164
Q

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

A

Group of code to be executed together. eg. function code block

165
Q

What does block scope mean?

A

A block scoped variable means that the variable defined within a block will not be accessible from outside the block

166
Q

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

A

Block-scoped

167
Q

What is the difference between let and const?

A

let can be reassigned, cont cannot

168
Q

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

A

You can change const but not reassign

169
Q

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

A

If the variable needs to be reassigned, use let. If not, use const.

170
Q

What is the syntax for writing a template literal?

A

backticks ( ` ) in lieu of single/double quotes
text content
$ { } for javascript expression
/n for new line

171
Q

What is “string interpolation”?

A

Substituting expression results or variables into a string

172
Q

What is destructuring, conceptually?

A

Taking pieces of data out of a variable and assigning it to a new variable

173
Q

What is the syntax for Object destructuring?

A

Const { property names, property name2, ….} = cont name

174
Q

What is the syntax for Array destructuring?

A

const [element, element1, element2] = array

175
Q

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

A

If { } or [ ] is on the left side of the assignment operator, then it’s destructuring
If on the right side, creating

176
Q

What is the syntax for defining an arrow function?

A

const functionName = ( ) => {

}

177
Q

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

A
178
Q

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

A

In an arrow function, value of this is determined when it’s being defined (vs. when it’s called)