JavaScript Flashcards

1
Q

What is the purpose of variables?

A

to store data values that can be used 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 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

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

letters, underscore, dollar sign, numbers (but variable name cannot start with a number)

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

variable names with different casing constitute different variables

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 words, letters, and characters

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 values

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 the value of true/false

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

What does the = operator mean in JavaScript?

A

assigns a value to 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

= assignment operator

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 must be intentionally assigned to a variable, while undefined is automatically assigned when a variable was not assigned a value

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 clear which variables are being logged and in what order

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

combines strings

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

addition for numbers and concatenation for 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

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

adds the value of the operand to the variable and assigns the result 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

group together related variables and functions

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

What are object properties?

A

variables that are part of an object which have a unique key name and correlated value

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

Describe object literal notation.

A

object in curly braces { } each key separated from its value using a colon, each key/value pair separated by commas

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 objectName.propertyName;

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

objectName.propertyName (dot notation) or objectName[“propertName”] (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

storing a list of values

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

Describe array literal notation.

A

square brackets [ ] each value is separated by a comma

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 are ordered and the keys are the index numbers (rather than the property names)

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

number of items in the 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

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

a block of code designed to perform a particular task

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, (optional) function name, comma-separated list of 0+ parameters surrounded by parentheses, curly braces, (optional) return statement

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 + parentheses with arguments (if function definition has them)

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

the definition has the function keyword and the code block with curly braces

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 placeholders in the function definition; arguments are passed to the function when it is called

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

Why are function parameters useful?

A

way to provide data to get different results from 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

1) causes the function to produce a value; 2) exits the 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 debug

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

What is a method?

A

a function that is a property of an 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

it is called on 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( )

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

Math.floor( )

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( ) or .pop( ) or .shift( )

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

no; can check by logging the string to the console or read up MDN

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

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

A

not necessarily

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

conditionally runs a block of code

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 keyword, condition in parentheses, curly braces

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

&& or ||

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 repeat a set of steps

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

to determine when the loop should stop

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

the code block being executed once

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

each time before the code block is executed

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

once at the beginning of the loop, before the condition

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

each time before the code block is executed

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

each time after the code block is executed

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

adds 1

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 ( var key in object )

66
Q

What is a “model”?

A

a representation of something

67
Q

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

A

the HTML document

68
Q

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

A

JavaScript objects which represent the different parts of the web page

69
Q

What is a DOM Tree?

A

an object model that represents the structure of a webpage

70
Q

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

A

querySelector( ) , getElementById( )

71
Q

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

A

querySelectorAll( )

72
Q

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

A

saves the browser from having to look through the DOM tree to find the same elements again

73
Q

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

A

console.dir( )

74
Q

Why would a < script > 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

75
Q

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

A

takes a CSS selector, returns node of the first matching element

76
Q

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

A

takes a CSS selector, returns a nodelist of all matching elements

77
Q

What is the purpose of events and event handling?

A

to enable the web page to interact with users

78
Q

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

A

optional

79
Q

What is a callback function?

A

a function passed into another function as an argument

80
Q

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

A

the event object that contains all relevant info about the event

81
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 where the event originated from; can read about it on MDN

82
Q

What is the className property of element objects?

A

enables you to get and set the value of the class attribute of the specified element

83
Q

What is the textContent property of element objects?

A

the text that is in the containing element (and its children)

84
Q

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

A

.textContent

85
Q

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

A

not necessarily because you don’t always need info about the event

86
Q

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

A

better way to save and store data values, such as info collected from users

87
Q

What does the transform property do?

A

allows you to modify the coordinate plane of the element

88
Q

Give four examples of CSS transform functions.

A

rotate, scale, skew, translate

89
Q

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

A

focus

90
Q

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

A

blur

91
Q

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

A

input

92
Q

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

A

submit

93
Q

What does the event.preventDefault() method do?

A

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)

94
Q

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

A

the browser will automatically reload the page with the form’s values in the URL

95
Q

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

A

elements

96
Q

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

A

value

97
Q

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

A

makes it difficult to debug, and you might have to rewrite all of it if there are errors

98
Q

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

A

check and debug code in real time

99
Q

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

A

no, it creates an element node but it is not on the page yet

100
Q

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

A

.appendChild( ) or .append( )

101
Q

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

A

(attributeName, attributeValue)

102
Q

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

A

createElement( ) , [ assign .textContent ] , appendChild( )

103
Q

What is the textContent property of an element object for?

A

to get/set the text content of an element and its children

104
Q

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

A

element.setAttribute(class, classValue) , or assign value as the className property

105
Q

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

A

so you can easily repeat the code block without having to redo the work; can make the website dynamic

106
Q

What is the event.target?

A

the element where the event originated from; a reference to the object onto which the event was dispatched

107
Q

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

A

hides the element and removes it from the document flow

108
Q

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

A

takes a CSS selector string, returns a boolean of whether the element is the selector

109
Q

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

A

Element.getAttribute( )

110
Q

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

A

after the value of a variable changes

111
Q

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

A

you would have to add an event listener for each tab

112
Q

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

A

you would have to manually set the class name for each tab and view

113
Q

What is a method?

A

a function that is a property of an object

114
Q

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

A

the method definition has a corresponding object property key, the function keyword, and curly braces which contain the function code block

115
Q

Describe method definition syntax (structure).

A

an object is defined with a property key and the corresponding function, which has the function keyword, parameters separated by commas in parentheses, and curly braces which contain the function code block

116
Q

Describe method call syntax (structure).

A

objectName.methodName(arg1, arg2,…)

117
Q

How is a method different from any other function?

A

it must be called on an object, can access data stored on the object

118
Q

What is the defining characteristic of Object-Oriented Programming?

A

pairs data with behavior

119
Q

What does API stand for?

A

Application Programming Interface

120
Q

What is “abstraction”?

A

simplifying a complex process so that it is easy to use

121
Q

What is the purpose of an API?

A

simplifies programming through abstraction

122
Q

What is this in JavaScript?

A

it is an implicit parameter

123
Q

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

A

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

124
Q

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

A

call time

125
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

the object named character

126
Q

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

A

“It’s-a-me, Mario!” - calling the greet method of the character property, which contains the firstName property of this object (the character object)

127
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!” - we do not know what the value of this will be since there is no object to the left of the dot

128
Q

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

A

the object in which the method is defined

129
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 dot; if there is no value to the left of the dot when the function is called, this will be the global window object (by default)

130
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based inheritance

131
Q

What is a prototype in JavaScript?

A

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

132
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

methods are defined on a “prototype” object and are borrowed when they’re needed

133
Q

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

A

the prototype object

134
Q

What does the new operator do?

A

1) creates a blank object 2) adds a __proto__ property to the new object that links to the constructor function’s prototype object 3) binds the newly created object instance as the this context 4) returns this if the function doesn’t return an object

135
Q

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

A

__proto__

136
Q

What does the instanceof operator do?

A

tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object (returns a boolean)

137
Q

What is a “callback” function?

A

a function passed into another function as an argument

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

139
Q

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

A

setInterval( )

140
Q

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

A

0

141
Q

What do setTimeout() and setInterval() return?

A

a positive integer value which identifies the timer created (ID)

142
Q

What is AJAX?

A

a technique for loading data into part of a page without having to refresh the entire page

143
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

144
Q

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

A

XMLHttpRequest

145
Q

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

A

both have the EventTarget prototype within their prototype chains

146
Q

What is the purpose of a conditional?

A

to make decisions in code

147
Q

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

A

the code within curly braces, such as the code block of functions, conditionals, and loops

148
Q

What does block scope mean?

A

the value of the variable is available only within the code block

149
Q

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

A

within the code block

150
Q

What is the difference between let and const?

A

variables declared with let can be reassigned while variables declared with const cannot

151
Q

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

A

because we are not reassigning a new value, just modifying the contents of the original value

152
Q

What is the syntax for writing a template literal?

A

text is wrapped in backticks ( ` )

153
Q

What is “string interpolation”?

A

substituting part of a string for the values of variables or expressions

154
Q

What is destructuring, conceptually?

A

a way to assign the parts of an object/array to individual variables

155
Q

What is the syntax for Object destructuring?

A

const/let { propertyName1: alias1, propertyName2: alias2 } = objectName;

156
Q

What is the syntax for Array destructuring?

A

const/let [ var1, var2, var3 ] = arrayName;

157
Q

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

A

the object/array literal is on the left-hand side of the = for destructuring, and it is on the right-hand side for creating

158
Q

What is the syntax for defining an arrow function?

A

parentheses around the parameters, followed by => , followed by curly braces containing the code block

159
Q

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

A

must be an expression - do not need a return statement

160
Q

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

A

the arrow function captures the this value of the enclosing context instead of creating its own this context