JavaScript Flashcards

1
Q

What is the purpose of variables?

A

So the computer has a temporary location 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

With the keyword ‘var’.

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

By using the assignment 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

No special characters besides $ or _. No spaces.

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

Name and name are two 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 a sequence of characters. Can be used when working with any kind of 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

For tasks that involve 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 help determine 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

The assignment operator in JavaScript means the data on the right is being assigned to the variable on the left.

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

By using the 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 is intentionally assigned to no value;

While undefined has not yet been 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 debugging easier.

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, symbol, bigInt.

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

Combining two or more strings into one.

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 and Concatenation.

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 whatever number is on the right to the variable on the left, and then assigns that 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

To organize a set of variables and functions under one reusable item.

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

What are object properties?

A

A variable within an object.

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

Describe object literal notation.

A

Curly brackets with all properties and methods inside, separated by commas.

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

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

A

Using dot or bracket notation.

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

How to you remove a property from an object.

A

The delete operator.

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 ordered lists of data.

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

Arrays are ordered and can be accessed numerically.

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

Determines how many items are in the array. Based on memory indexes.

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

How do you calculate the last index of an array?

A

Length of the array minus one.

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 “formula” used as a way for us to repeat code throughout our program without having to type that code out each time.

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, followed by an optional name, parameters enclosed in parentheses, code-block enclosed with curly-braces.

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

The function name, followed by parentheses and any necessary parameters.

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 code-block does not run when a function is defined, only when it is called.

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

A parameter is a placeholder variable inside a function definition. An argument is a variable passed to a function when it’s called.

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

Why are function parameters useful?

A

So we can change the data we give to our 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

It causes the function to end, and returns a value in it’s place.

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 assist in debugging.

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

What is a method?

A

A function within 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 within 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

With the 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

With the floor( ) method of the Math object.

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

With the random( ) method of the Math object.

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

With the splice( ) method.

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

How do you append an element to an array?

A

With the push( ) method.

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

How do you break a string up into an array?

A

With the split( ) method

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

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

A

They do not; And you could console log the original string after to be sure.

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

A LOT.

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

Not in every situation. Such as console.logs.

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

A LOT.

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

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 a condition before running a segment of code.

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 ( condition ) { conditional code-block };

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

What are the three logical operators?

A

AND ( && ), OR ( || ), NOT ( ! )

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

By using logical operators.

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 repeat segments of code.

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 check if 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 time the loop is run is 1 iteration.

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 loop runs.

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

The initialization is the first thing to get evaluated in a loop. Only happens once.

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 the loop is run.

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 the loop has run, unless otherwise specified.

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

Increments the variable by one and adds it to itself.

66
Q

How do you iterate through the keys of an object?

A

with a for…in loop

67
Q

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

A

To make things easier to understand/debug.

68
Q

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

A

JavaScript objects.

69
Q

What is a DOM Tree?

A

A tree of objects created by JavaScript to interact with HTML.

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

If you need to work with an element more than once.

73
Q

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

A

console.dir( )

74
Q

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

A

It takes a CSS Selector as its argument and returns the first match.

75
Q

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

A

It takes a CSS Selector as its argument and returns a NodeList of all matches in the document.

76
Q

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

A

So it runs after the HTML has loaded.

77
Q

What is the purpose of events and event handling?

A

To detect things such as clicks and change what the user sees. (To make things interactive.)

78
Q

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

A

It means optional.

79
Q

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

A

.addEventListener( )

80
Q

What is a callback function?

A

A function defined elsewhere in the code, passed as an argument.

81
Q

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

A

An object.

82
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 event.target property is a reference to the object onto which the event was dispatched.

83
Q

What is the className property of element objects?

A

A string containing all the classes of that element.

84
Q

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

A

By using the className property.

85
Q

What is the textContent property of element objects?

A

A string encompassing all text inside an element.

86
Q

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

A

By assigning a new string to the textContent property of a variable.

87
Q

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

A

No. For instance when you are using the function as an argument.

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

89
Q

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

A

It’s much easier for the computer compared to having to search the DOM each time.

90
Q

What does the transform property do?

A

Changes the way an item appears visually.

91
Q

Give four examples of CSS transform functions.

A

Translate, skew, rotate, and scale.

92
Q

The transition property is shorthand for which four CSS properties?

A

Transition-property, transition-duration, transition-timing-function, and transition-delay.

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

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

Refresh the page.

99
Q

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

A

elements property.

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

You may have to go back and rewrite a lot of it.

102
Q

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

A

You can test as you go.

103
Q

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

A

No, it just creates it in JavaScript.

104
Q

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

A

With the appendChild( ) method.

105
Q

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

A

The attribute name and value.

106
Q

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

A

createElement( ), setAttribute( ) / .textContent, appendChild( )

107
Q

What is the textContent property of an element object for?

A

To tell what text is inside the element.

108
Q

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

A

using setAttribute( ) method, and the className( ) method.

109
Q

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

A

It can be reused again and again. It simplifies things for the computer.

110
Q

Give two examples of media features that you can query in an @media rule.

A

width, height, aspect-ratio

111
Q

Which HTML meta tag is used in mobile-responsive web pages?

A

Viewport.

112
Q

What is the event.target?

A

A reference to the object onto which the event was dispatched.

113
Q

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

A

Because of event bubbling.

114
Q

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

A

event.target.tagName

115
Q

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

A

Searches the element and its parents to find a matching selector string. returns itself or the matching ancestor, or null if no matches are found.

116
Q

How can you remove an element from the DOM?

A

With the remove( ) method.

117
Q

What is the event.target?

A

A reference to the object onto which the event was dispatched.

118
Q

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

A

Removes it from the layout.

119
Q

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

A

Takes a selector string as an argument, returns a boolean value.

120
Q

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

A

Using the getAttribute( ) method.

121
Q

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

A

Any time decisions are made or variables are assigned.

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 to add eventListeners to individual elements.

123
Q

How to you store data in localStorage?

A

With the setItem( ) method.

124
Q

How to you retrieve data from localStorage?

A

With the getItem( ) method.

125
Q

What data type can localStorage save in the browser?

A

Strings.

126
Q

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

A

When the window is about to be unloaded.

127
Q

What is a method?

A

A function which is a property of an object.

128
Q

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

A

Method definitions are prefaced with the keyword function and followed by a code block

129
Q

Describe method definition syntax (structure).

A

Name of the property first, followed by a colon, and then an anonymous function definition.

130
Q

Describe method call syntax (structure).

A

Say the object first, then dot, then the method name.

131
Q

How is a method different from any other function?

A

It is a property of an object.

132
Q

What is the defining characteristic of Object-Oriented Programming?

A

Using objects to contain both data and behavior.

133
Q

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

A

Abstraction, Encapsulation, Inheritance, Polymorphism

134
Q

What is “abstraction”?

A

Being able to work with complex things in seemingly simple ways.

135
Q

What does API stand for?

A

Application Programming Interface

136
Q

What is the purpose of an API?

A

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

137
Q

What is this in JavaScript?

A

A reference to the object where the function or method is being called.

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

139
Q

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

A

Call time.

140
Q

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

A

‘this’ is not determined during definition.

141
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype-based inheritance

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

Because they are methods of its prototype.

143
Q

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

A

On its prototype.

144
Q

What does the new operator do?

A

The new operator 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.

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

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.

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

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

With the setTimeout( ) method.

149
Q

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

A

By using the setInterval( ) method.

150
Q

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

A

0 milliseconds.

151
Q

What do setTimeout() and setInterval() return?

A

A positive integer used as an ID to clear the timeout or interval.

152
Q

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

A

Code blocks are denoted by curly braces { }; Follows the condition in if/else statements and the parameter list in function definitions.

153
Q

What does block scope mean?

A

It means a variable defined inside a block can only be referenced from within that same block.

154
Q

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

A

Block scope

155
Q

What is the difference between let and const?

A

The const keyword creates block-scoped variables whose values can’t be reassigned, while variables created with let are mutable.

156
Q

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

A

Because the reference to the array is constant, but the values within that array are still mutable.

157
Q

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

A

If a variable does not need to be reassigned, you should use a const; If it does, use let.

158
Q

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

A

A function.

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

Defines an anonymous function receiving one parameter, value, which returns an anonymous function receiving no parameters and returning the variable value.
This is all being assigned to the constant variable wrap.

160
Q

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

A

When it is defined.