JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Used to store data

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 variableName;

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 variableName = “string”

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, numbers, $ , and _.

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

S != s. Uppercase and lowercase letters will be interpreted differently.

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

Consists of letters and other characters. They are frequently used to add new content to a page and they can contain HTML markup

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

Numeric data types handle numbers.

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

Have one of two values: true or false. Booleans are helpful when determining which part of a script should run.

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

= assigs a value to a variable

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

assign a new value to the variable

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

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

A

To avoid confusion of having multiple console.logs

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

What is string concatenation?

A

combining multiple strings together

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

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

A

concatenation and addition

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

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

A

boolean

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

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

A

Adds by a specified value and sets the variable to the value of the expression

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

What are objects used for?

A

storing key-value pairs. Used to model real-life objects

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

What are object properties?

A

storage areas connected to an object that can store data

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

Describe object literal notation.

A

{key: value}

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

What are arrays used for?

A

Storing data. Particularly useful for lists.

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

Describe array literal notation.

A

[ value, value,… ]

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

How are arrays different from “plain” objects?

A

They are numerically indexed

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

What is the length property of an array?

A

.length

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

How do you calculate the last index of an array?

A

array.lenght - 1

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

What is a function in JavaScript?

A

collection instructions that can be repeated whn called

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

Describe the parts of a function definition.

A

function keyword, name, parameter, , the start of the code block, return statement, end of the code block

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

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

A

function call takes arguments in parentheses. a function definition has parameters and code block

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

What is the difference between a parameter and an argument?

A

a parameter is a place holder (variable). arguments are the values passes to a parameter

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

Why are function parameters useful?

A

allows you to pass information to a function

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

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

A

returns a value to function and stops execution of code

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

Why do we log things to the console?

A

for debugging

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

What is a method?

A

Actions that are performed on objects

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

How is a method different from any other function?

A

a method exists as a property on an object. a function can be defined anywhere

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

How do you remove the last element from an array?

A

.pop( ) method

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

How do you round a number to the nearest integer?

A

.round( ) method

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

How do you generate a random number?

A

.random( ) method of the Math object

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

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

A

No. check with console.log.

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

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

A

40 - 50

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

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

A

No. Some functions just modify and change things. Others are used to return something

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

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

A

A lot

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

Give 6 examples of comparison operators.

A

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

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

What is the purpose of an if statement?

A

Evaluates a condition to determine if code will be run

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

What are the three logical operators?

A

&& || !

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

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

A

With a logical operator

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

What is the purpose of a loop?

A

to run a block of code until a certain condition is met

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

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

A

determines whether or not the code is run depending on boolean value

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

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

A

each pass through a loop

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

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

A

at the beginning of each iteration

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

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

A

before any code has been 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 for loop get evaluated?

A

at the beginning of each iteration

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

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

A

at the end of each loop

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

What does the ++ increment operator do?

A

The increment operator (++) increments (adds one to) its operand and returns a value.

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

Why do we log things to the console?

A

For debugging and checking functionality

67
Q

What is a “model”?

A

A representation of an object

68
Q

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

A

An HTML Page

69
Q

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

A

Each object represents a different part of the page loaded in the browser window

70
Q

What is a DOM Tree?

A

Representation of the DOM that is made up of nodes (document node, element nodes, attribute nodes, and text nodes).

71
Q

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

A

getElementByID( ) and querySelector( )

72
Q

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

A

querySelectorAll( )

73
Q

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

A

It is helpful when you want to work with the element more than once

74
Q

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

A

console.dir( )

75
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 parse all of the elements in the HTML page before the JavaScript code can access them.

76
Q

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

A

A CSS selector is taken as an argument and the first matching element is returned

77
Q

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

A

A CSS selector is taken as an argument and all matching elements are returned in a node list

78
Q

Why do we log things to the console?

A

For debugging and checking functionality

79
Q

What is the purpose of events and event handling?

A

To allow the script to respond when certain events and interactions occur with the page

80
Q

What do [] square brackets mean in function and method syntax documentation?

A

It means it is optional

81
Q

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

A

addEventListener( )

82
Q

What is a callback function?

A

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.

83
Q

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

A

the event object

84
Q

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

A

The target property of the Event interface is a reference to the object onto which the event was dispatched. Check MDN documentation to get more information about it.

85
Q

What is the difference between these two snippets of code?

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

A

The function with parentheses would indicate that it should run when the page loads. The function without parentheses will run when the event is fired.

86
Q

What is the className property of element objects?

A

The className property gets the value of the class attribute of the specified element.

87
Q

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

A

.className property

Element.className = cName

88
Q

What is the textContent property of element objects?

A

represents the text content of the node and its descendants.

89
Q

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

A

.textContent property

Element.textContent = ‘ ‘

90
Q

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

A

Generally useful

91
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

More complicated

92
Q

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

A

You can access it quicker. Do not rely on the DOM.

93
Q

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

A

focus

94
Q

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

A

blur

95
Q

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

A

input

96
Q

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

A

submit

97
Q

What does the event.preventDefault() method do?

A

preventDefault() method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

98
Q

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

A

the page would reload.

99
Q

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

A

form.elements

100
Q

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

A

value

101
Q

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

A

It will be difficult to find where your code stopped working

102
Q

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

A

gives you the ability to check code

103
Q

What is the event.target?

A

a reference to the object onto which the event was dispatched

104
Q

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

A

Turns off the display of an element and acts as if it was not there.

105
Q

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

A

a selector is used as an arguments. Returns a boolean

106
Q

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

A

getAttribute( )

107
Q

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

A

Everywhere

108
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 need an event listener for each tab and view

109
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 need conditionals for each

110
Q

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

A

event bubbling

111
Q

What is the event.target?

A

a reference to the object onto which the event was dispatched

112
Q

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

A

tagName

113
Q

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

A

A selector is used as an argument. It returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. If there isn’t such an ancestor, it returns null.

114
Q

How can you remove an element from the DOM?

A

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

using the parent node as the eventlistener

116
Q

What is JSON?

A

JSON is a text-based data format following JavaScript object syntax. JSON exists as a string — useful when you want to transmit data across a network.

117
Q

What are serialization and deserialization?

A

Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.

Deserialization is the reverse process: turning a stream of bytes into an object in memory.

118
Q

Why are serialization and deserialization useful?

A

By allowing an Object to be converted into stream of bytes so that it can be transferred over a network or stored in a persistent storage. Vis versa, the string can be pulled from a network and converted to an object.

119
Q

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

A

JSON.stringify

120
Q

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

A

JSON.parse

121
Q

How to you store data in localStorage?

A

.setItem( )

122
Q

How to you retrieve data from localStorage?

A

.getItem( )

123
Q

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

A

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.

124
Q

What is a method?

A

A method is a function which is a property of an object. There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or Static Methods which are tasks that are called directly on an object constructor.

125
Q

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

A

Method call-
object.method()

method definition-
   method: function() {
       //
   }
126
Q

Describe method definition syntax (structure).

A
method: function() {
       //
   }
127
Q

Describe method call syntax (structure).

A

object.method()

128
Q

How is a method different from any other function?

A

A method is associated with an object while a function is not

129
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

130
Q

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

A

Abstraction, Encapsulation, Inheritance, Polymorphism

131
Q

What is “abstraction”?

A

Being able to work with (possibly) complex things in simple ways.

132
Q

What does API stand for?

A

Application programming interface

133
Q

What is the purpose of an API?

A

the purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

134
Q

What data type can local storage save in the browser

A

String

135
Q

What is this in JavaScript?

A

a keyword which contains a value which is defined at call time

136
Q

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

A

it is available in a function’s code block even though it was never included in the function’s parameter list or declared with var.

137
Q

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

A

call time

138
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

Mario

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

Given the above character object, what is the result of the following code snippet? Why?

character.greet();

A

“It’s-a-me Mario!”

The greet method of the character object is call. This method includes a string that is concatenated with this.FirstName.

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

Given the above character object, what is the result of the following code snippet? Why?

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

undefined.

this is attached to hello when it is called with hello. hello does not have a firstName property. this is attached to whoever is calling the function.

141
Q

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

A

it will refer to the object it is in

142
Q

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

A

the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).

143
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype based

144
Q

What is a prototype in JavaScript?

A

An object that contains properties and (predominantly) methods that can be used by other objects.

145
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

The methods are defined on a “prototype” object and arrays simply borrow those methods when they’re needed.

146
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 chain

147
Q

What does the new operator do?

A
  1. Creates a blank, plain JavaScript object;
  2. Links (sets the constructor of) the newly created object to another object by setting the other object as its parent prototype;
  3. Passes the newly created object from Step 1 as the this context;
  4. Returns this if the function doesn’t return an object.
148
Q

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

A

prototype property

149
Q

What does the instanceof operator do?

A

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

150
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.

151
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( )

152
Q

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

A

setInterval( )

153
Q

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

A

0

154
Q

What do setTimeout() and setInterval() return?

A

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout();

The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval()

155
Q

What is Array.prototype.filter useful for?

A

Creating a new array from an existing array that contains the values that pass a given callback function

156
Q

What is Array.prototype.map useful for?

A

creating a new array containing the values resulting from calling a provided callback function on each value of an array

157
Q

What is Array.prototype.reduce useful for?

A

for reducing an array to a single value using a provided callback function

158
Q

What does fetch() return?

A

a promise

159
Q

What is the default request method used by fetch()?

A

GET

160
Q

How do you specify the request method (GET, POST, etc.) when calling fetch?

A

specify the request method as the value of the method property in the init object

161
Q

What must the return value of myFunction be if the following expression is possible?
myFunction()();

A

a function

162
Q
What does this code do?
const wrap = value => () => value;
A

a function that takes a single parameter and returns a function that returns the value of that parameter

163
Q

In JavaScript, when is a function’s scope determined; when it is called or when it is defined?

A

when it is defined

164
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

closures