JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store information, so it can be retrieved 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

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

name = 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

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (0–9).

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

Their exact case must match.

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 hold 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 hold information that relates to arithmetic calculations.

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 check whether a value is true or false and make decisions based on that.

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

Assigning a value.

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 = newValue;

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 has been set by the user, while undefined is on the computers end.

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 should be able to see clearly what relates to what, and also for the benefit of others.

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

Give five examples of JavaScript primitives.

A

number / string / 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

Adding a string to another string using +

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

Concatenation of strings / addition

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.

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 new value, and reassigns to the variable.

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 properties and methods together.

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

What are object properties?

A

Like variables, they contain information about specifics of an object.

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

Describe object literal notation.

A

{ }

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

object.property / object[‘property’]

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

What are arrays used for?

A

To store lists.

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

Describe array literal notation.

A

[ ]

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

They have a specific order.

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

How many items the array contains.

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

array.length - 1

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

Why should arrays have similar data types and structure?

A

Will be much easier to work with, and loop over.

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

What is a function in JavaScript?

A

A recipe, a set of steps.

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

Describe the parts of a function definition.

A
function functionName(params) {
  code
  optional return
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Describe the parts of a function call.

A

functionName(params);

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

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

A
function keyword.
code block { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is the difference between a parameter and an argument?

A

parameter is a placeholder, no value yet. Argument is when we are giving it a value.

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

Why are function parameters useful?

A

To supply additional information, to make it reusable

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

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

A

Code stops running there.

Returns a value that can be used.

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

Why do we log things to the console?

A

To see if they behave as expected.

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

What is a method?

A

A function of an object.

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

How is a method different from any other function?

A

Since it belongs to an object, it needs to be called with the object.

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

How do you remove the last element from an array?

A

array.pop( )

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

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

A

Math.floor( )

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

How do you generate a random number?

A

Math.random ( )

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

How do you delete an element from an array?

A

array.splice(1, 1, add)

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

How do you append an element to an array?

A

array.push( )

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

How do you break a string up into an array?

A

string.split(‘ ‘)

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

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

A

No

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

No

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

Give 6 examples of comparison operators.

A

=== / !== / < / > / <= / >=

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

What is the purpose of an if statement?

A

To check whether something is true, and do something based on that

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

Describe the syntax (structure) of an if statement.

A

if (expression) {
code
}

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

What are the three logical operators?

A

&& / || / !

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

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

A

With && ||

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

What is the purpose of a loop?

A

To do steps repeatedly until a certain condition is met.

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

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

A

To see whether the loop should continue.

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

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

A

Each round

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

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

A

Before the code block begins

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

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

A

First thing

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

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

A

Before each iteration of the loop

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

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

A

After each iteration of the loop

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

What does the ++ increment operator do?

A

Adds 1

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

How do you iterate through the keys of an object?

A

for (var key in object)

67
Q

What is a “model”?

A

Recreation of something, in this case, the HTML page

68
Q

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

A

The HTML document

69
Q

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

A

Javascript objects representing elements on the page

70
Q

What is a DOM Tree?

A

The presentation of the nodes in a tree-like structure

71
Q

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

A

getElementById / 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

To be able to use it again in the future

74
Q

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

A

dir

75
Q

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

A

So that it can first read all elements and then take any action required

76
Q

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

A

css selector, returns the first match

77
Q

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

A

css selector, returns all matches as a node list

78
Q

What is the purpose of events and event handling?

A

So that when events happen, we can have something which handles it

79
Q

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

A

Optional

80
Q

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

A

addEventListener

81
Q

What is a callback function?

A

A function which is called by another function

82
Q

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

A

event

83
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 element that fired the event. MDN

84
Q

What is the difference between these two snippets of code?

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

A

1) We are telling the browser to call the function

2) We are calling the function and getting its return which is undefined

85
Q

What is the className property of element objects?

A

Can get or set the classname on an element

86
Q

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

A

var.className

87
Q

What is the textContent property of element objects?

A

Can get or set the text on an element

88
Q

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

A

var.textContent

89
Q

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

A

No

90
Q

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

A

Easier to retrieve

91
Q

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

A

focus

92
Q

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

A

blur

93
Q

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

A

input

94
Q

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

A

submit

95
Q

What does the event.preventDefault() method do?

A

That the page shouldn’t refresh without hitting the submit button

96
Q

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

A

The page would refresh.

97
Q

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

A

.elements

98
Q

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

A

.name.value

99
Q

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

A

No

100
Q

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

A

appendChild( )

101
Q

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

A

name, value

102
Q

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

A

createElement, Add to the Dom

103
Q

What is the textContent property of an element object for?

A

To add or get the text content

104
Q

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

A

element.className / element.setAttribute( )

105
Q

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

A

Reusable / Easier debugging / returns value

106
Q

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

A

Event bubbling

107
Q

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

A

event.target.tagName

108
Q

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

A

css selector, it returns the closest parent

109
Q

How can you remove an element from the DOM?

A

element.remove( )

110
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

event delegation on the parent

111
Q

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

A

Hides it

112
Q

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

A

css selector / boolean

113
Q

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

A

getAttribute( )

114
Q

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

A

All steps

115
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

Requiring to add an extra event handler for each

116
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

Each step would have to be manually repeated

117
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

118
Q

What are serialization and deserialization?

A

Making it into a string / Making the string into a object

119
Q

Why are serialization and deserialization useful?

A

To send over the network / To be able to use in more dynamic ways (objects, arrays)

120
Q

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

A

JSON.stringify( )

121
Q

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

A

JSON.parse( )

122
Q

How do you store data in localStorage?

A

localStorage.setItem( )

123
Q

How to you retrieve data from localStorage?

A

localStorage.getItem( )

124
Q

What data type can localStorage save in the browser?

A

Only string

125
Q

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

A

Before you leave the page or refresh

126
Q

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

A

method definition = function keyword / code block

method call = obj.methodName( )

127
Q

Describe method definition syntax (structure).

A

propertyName: function( ) {
code
}

128
Q

Describe method call syntax (structure).

A

obj.methodName( )

129
Q

How is a method different from any other function?

A

It belongs to an object

130
Q

What is the defining characteristic of Object-Oriented Programming?

A

Data together with methods form objects

131
Q

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

A

Abstraction / Encapsulation / Inheritance / Polymorphism

132
Q

What is “abstraction”?

A

Hiding the implementation of a complex process

133
Q

What does API stand for?

A

Application Programming Interface

134
Q

What is the purpose of an API?

A

To be able to access a complex process without needing to know how it works

135
Q

What is this in JavaScript?

A

An implicit parameter.

136
Q

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

A

It is available even though it has not been specifically defined

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);
  }
};

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

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

The character object

It’s me Mario, because the character object is calling it.

It’s me undefined, because the default window object is calling it.

139
Q

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

A

You can’t

140
Q

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

A

The object to the left of the .

141
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype

142
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

Through getting it from the prototype

143
Q

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

A

In the prototype

144
Q

What does the new operator do?

A

Creates a blank, plain JavaScript object;
Links the newly created object to another object by setting the other object as its parent prototype;
Passes the newly created object as the this context;
Returns this if the function doesn’t return an object.

145
Q

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

A

.prototype

146
Q

What does the instanceof operator do?

A

Checks whether that object is an instance of another object

147
Q

What is a “callback” function?

A

A function within a function

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

149
Q

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

A

setInterval ( )

150
Q

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

A

0

151
Q

What do setTimeout() and setInterval() return?

A

The id associated with them

152
Q

What is a client?

A

A software or computer requesting information

153
Q

What is a server?

A

Something that is able to provide a service

154
Q

Which HTTP method does a browser issue to a web server when you visit a URL?

A

GET

155
Q

What three things are on the start-line of an HTTP request message?

A

http method / Url / Protocol

156
Q

What three things are on the start-line of an HTTP response message?

A

Protocol / Status code/ Status text

157
Q

Where would you go if you wanted to learn more about a specific HTTP Header?

A

MDN

158
Q

Is a body required for a valid HTTP request or response message?

A

No

159
Q

What is AJAX?

A

A programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest.
Ajax allows you to update parts of the DOM of an HTML page instead without the need for a full page refresh. Ajax also lets you work asynchronously, meaning your code continues to run while the targeted part of your web page is trying to reload (compared to synchronously, which blocks your code from running until that part of your page is done reloading).

160
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

161
Q

Which object is built into the browser for making HTTP requests in JavaScript?

A

XMLHttpRequest

162
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

load

163
Q

An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

Share the same prototype

164
Q

What are HTTP headers?

A

Additional information relating to a request or response