Javascript Flashcards

1
Q

What is the purpose of variables?

A

To give us a location to store data to use in the future

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

How do you declare a variable?

A

By using var, setting a variable name, then using the = operator to set it to a value

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

Using the = operator

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, dollar sign, underscore

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

It is sensitive to capitalizations

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 a numerical or mathematical 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 a true or false value

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

It’s used to set a variable to a value (assignment 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

State the variable name, assignment operator, set it to new 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 assigned value and explicitly means nothing. Undefined means a variable has been declared, but the value has not yet been defined

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

It adds readability and gives more context

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

A number data type

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

What is string concatenation?

A

When you combine multiple strings using the + operator

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

To perform addition or concatenate multiple strings together to create one value

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

A shorthand operator that allows you to add the value from the right operand to the left operand, then assigns the new value to the left operand

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 multiple data points or properties within a variable

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

What are object properties?

A

A collection of values attached to an object

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

Describe object literal notation.

A

A variable assigned to multiple properties using curly brackets, then a list of properties and their values separated by a colon, comma to separate multiple properties.

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

Keyword “delete” then the object.property or object[‘property’]

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

getAttribute, setAttribute

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 a list of elements that we can access by a single variable. To-do lists, lists of users, etc are all great ways to leverage arrays

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

Describe array literal notation.

A

Declare var, variable name, assignment operator, square brackets, and put each value in the brackets, separated by commas

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

Objects use properties and values, while arrays have values only. Objects don’t have an order, while arrays do. Objects use dot notation while arrays use bracket notation.

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

It tells you how many items are in your 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

Array.length minus 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

A set of statements that performs a task or calculates a value

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

Function keyword, function name, parenthesis for parameters, curly bracket for function code block, return keyword, then expression, closed curly bracket to close declaration

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

Function name, parenthesis, arguments.

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

Parameters vs arguments. Function definition has function key work and code block. Function call contains the function name and parenthesis to call it.

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

Parameters are properties of a function definition. Arguments are properties of a function call.

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

Why are function parameters useful?

A

Parameters allow us to pass information or instructions into functions

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

A return statement ends the execution of a function, and returns control to the calling function

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 ensure the code is working properly and to debug code

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

What is a method?

A

a property of an object that contains a function definition

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

A method has to be attached to an object

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

Pop method

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

math.Random

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

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

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

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

They do NOT change the original string. Do it then console log it.

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

40 or so

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

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

A

30 or so

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 guide a program to make decisions based on specified criteria

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 keyword, parenthesis for condition, curly brace for condition code block, return

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 !

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

Logical operators

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

Loops allow you to repeat a process over and over without having to write the same (potentially long) instructions each time you want your program to perform a task.

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

an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps 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

Is the single time a loop code block executes

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

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

Beginning of the loop

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

Before each iteration takes place

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 takes place

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

Increases the value of a variable by 1, then substitutes the new value

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 see what the code is doing / debugging

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

What is a “model”?

A

Contains all the logic in Javascript

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

The 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

A model of the document being represented by javascript objects

72
Q

What is a DOM Tree?

A

A representative chunk of the page built as javascript objects

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

getElementsByClassName() & querySelectorAll()

75
Q

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

A

To use again or reference later

76
Q

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

A

Console.dir (directory method)

77
Q

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

A

The HTML document loads from top to bottom. It needs to come after the HTML elements so the DOM content is there before the script loads.

78
Q

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

A

It takes a string as an argument (CSS selector) and returns the first instance of that element that matches in the HTML document

79
Q

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

A

It takes a string as an argument (CSS selector) and returns a node list. A node list is not a DOM element.

80
Q

Why do we log things to the console?

A

To see what the code is doing. Great debugging tool.

81
Q

What is the purpose of events and event handling?

A

To react to something the user does. Event handling is the setup of the response of what happens when the user does something.

82
Q

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

A

No. You can use just the one or ones that are relevant for the specific task

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

84
Q

What is a callback function?

A

A function definition being passed as an argument to another function

85
Q

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

A

The event object, or the object with all the information about the event that just occurred

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

It stores the DOM element where the event originated from (the start point of the event). Console log it.

87
Q

What is the difference between these two snippets of code?

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

A

The first is setting up an event listener that calls the function handleClick when a user clicks. The second is executing the function handleClick right this second. There is no return so it will be undefined.

88
Q

What is the className property of element objects?

A

used to set or return the value of an element’s class attribute

89
Q

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

A

Use className or setAttribute

90
Q

What is the textContent property of element objects?

A

Represents the text content of the node and it’s descendants

91
Q

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

A

textContent

92
Q

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

A

No. If you know what you want to do you probably don’t need it.

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

More complicated

94
Q

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

A

Organization, simplicity, and to refer to it later

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 form?

A

Submit

99
Q

What does the event.preventDefault() method do?

A

Prevents the default behavior from happening

100
Q

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

A

It would refresh the page because it doesn’t stop the default action

101
Q

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

A

HTMLFormElements

102
Q

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

A

Value

103
Q

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

A

If you get an error, you won’t know where or why it’s not working

104
Q

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

A

You can see errors in real time which allows you to know exactly where in the code it’s happening

105
Q

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

A

No. It creates a new element node, but it’s not attached to the DOM yet

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

Two strings. The first being the name of the attribute, the second is the value of the attribute

108
Q

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

A

Query the parent element, create a new element, append new element to parent element

109
Q

What is the textContent property of an element object for?

A

to set text content for the HTML element or get the text content written inside that element

110
Q

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

A

setAttribute method or className

111
Q

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

A

Makes it reusable. Debugging.

112
Q

What is the event.target?

A

The target event property returns the element that triggered the event

113
Q

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

A

Event bubbling

114
Q

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

A

tagName property

115
Q

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

A

It takes a selector as an argument and returns the closest ancestor Element or itself, which matches the selectors

116
Q

How can you remove an element from the DOM?

A

The remove method on the element we want to remove

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

Dom event delegation. Put eventlistener on the parent.

118
Q

What is the event.target?

A

The target event property returns the element that triggered the event.

119
Q

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

A

It takes a CSS selector as a string and returns a boolean

120
Q

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

A

getAttribute method

121
Q

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

A

At every step

122
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 go create event listeners for each element

123
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’d have to write a bunch of conditionals

124
Q

What is JSON?

A

JSON stands for JavaScript Object Notation. Converts objects and arrays into a string for data transfer.

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

126
Q

Why are serialization and deserialization useful?

A

It allows us to transfer data in a uniform way

127
Q

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

A

Use the stringify method of the JSON object. Pass in an argument that will be an object or array and the return of the method will be a string.

128
Q

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

A

Use the parse method of the JSON object. Pass in a string as an argument and it will return an object or an array.

129
Q

How do you store data in localStorage?

A

setItem method of the local storage object. Arguments are a string of the name of the key, and the string of the value.

130
Q

How do you retrieve data from localStorage?

A

getItem method of the localStorage object. The argument is the key of the value you want to retrieve.

131
Q

What data type can localStorage save in the browser?

A

Strings

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

133
Q

What is a method?

A

A method is a function stored in a property of an object

134
Q

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

A

A method definition contains the word ‘function’ to define it with a code block that contains the logic for the method, while the method call is the name of the function followed by ()

135
Q

Describe method definition syntax (structure).

A

Object name, property, colon, function definition

136
Q

Describe method call syntax (structure).

A

object.Method name with ()

137
Q

How is a method different from any other function?

A

It’s stored on properties of objects only

138
Q

What is the defining characteristic of Object-Oriented Programming?

A

In OOP, objects can contain both data and behavior. Methods can use both within the object.

139
Q

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

A

encapsulation, abstraction, inheritance and polymorphism

140
Q

What is “abstraction”?

A

hide the implementation details and highlight an object’s essential features to the users

141
Q

What does API stand for?

A

Application programming interface

142
Q

What is the purpose of an API?

A

to give programmers a way to interact with a system in a simplified, consistent fashion.

143
Q

What is this in JavaScript?

A

‘This’ refers to the object it’s being invoked on

144
Q

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

A

The object on which the method is called. It is called implicit because we don’t declare it when we declare the method.

145
Q

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

A

Call time

146
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

‘This’ is nothing because the greet function was never called

147
Q

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

A

“It’s-a-me Mario!” Because the method is being called on the character object.

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

It will return undefined because hello() is not attached to an object

149
Q

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

A

You don’t know

150
Q

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

A

If the method is attached to an object on the left using dot notation, ‘this’ will return values from the object

151
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypal inheritance

152
Q

What is a prototype in JavaScript?

A

Prototypes are the mechanism by which JavaScript objects inherit features from one another

153
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

setprototypeof() on the object

154
Q

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

A

It will look for it in the prototype

155
Q

What does the new operator do?

A

lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.In short, Creates a new blank object, sets the prototype, makes the new object this, then returns the object at the end of the object.

156
Q

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

A

Prototype property which stores an object

157
Q

What does the instanceof operator do?

A

check the type of an object at the run time

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

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

160
Q

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

A

setInterval()

161
Q

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

A

There is no time delay

162
Q

What do setTimeout() and setInterval() return?

A

Returns an ID that is a positive integer value which identifies the timer created by the call

163
Q

What is a client?

A

A program that is requesting data

164
Q

What is a server?

A

A program that provides data

165
Q

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

A

GET

166
Q

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

A

HTTP method (GET, PUT, POST), request target, and HTTP version

167
Q

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

A

Protocol version, status code, and status text

168
Q

What are HTTP headers?

A

a field of an HTTP request or response that passes additional context and metadata about the request or response

169
Q

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

A

MDN

170
Q

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

A

No

171
Q

What is AJAX?

A

Allows you to request data from a server and load it without having to refresh the entire page

172
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

173
Q

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

A

XMLHttpRequest

174
Q

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

A

Load

175
Q

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

A

Because of prototypal inheritance