JavaScript Flashcards

1
Q

What is the purpose of variables?

A

to hold data (numbers, strings, booleans)

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 creating a variable 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

adding an equal sign next the variable name and then giving it a value/data

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 but we shouldn’t use numbers

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 means that the capitalization needs to be the same in order to call that variable (ex. var myName and var MyName is 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

it holds the data type of letters and other 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

it holds the data type of numbers (integers, decimals, etc)

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

it holds the data type of one of two values, true or 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

a value is being assigned 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

by assigning it to a 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 subject to change, there is nothing but it could change into something. while undefined is literally nothing

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 won’t get confused to what is being logged

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

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

What is string concatenation?

A

when two or more strings are added together

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

used to add one value to another

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

it adds and re-assigns a new value to a 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 hold a set of variables that all link to one thing/object

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

What are object properties?

A

it tells us about the object, stores info ab the object (ex. name:, age:,)

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

Describe object literal notation.

A

an array of keys and values

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

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

using a dot notation or bracket notation (ex. example.name, example[‘name’])

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

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

Describe array literal notation.

A

values within brackets [ ]
(ex. var colors = [‘red’, ‘white’, ‘blue’]

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 store actual variables in an index starting from 0

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 shows the amount of variables inside of an 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

by subtracting the length of an array by 1
(ex. var lastIndex = example.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

it is a “power” tool, it is used to run a sequence of code when it is called

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

a function definition is the name used for that function so you have something to call it, also contains parameters
(ex. function sayHello(name) {} )

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

you can call a function using the function definition and parenthesis (ex. sayHello(‘Tim’) )

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

function call has the value inside(a parameter) the parenthesis while the function definition has a variable (argument) inside

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 like a placeholder for the value like a variable, an argument has a value. when we define a function we declare a parameter, when we call a function we pass it arguments

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

Why are function parameters useful?

A

it’s a placeholder so that we know what will come out as an argument

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 produces a value for us to use and it prevents any more code in the function’s code block to run

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

so that we able to see what the code is producing

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

What is a method?

A

it is a function which 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

methods are associated with objects while function just performs a task when called

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

using 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

using the Math.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

using the Math.random() method

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

using the slice() , pop(), shift() 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

using 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

using 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

string methods doesn’t change the original string. to double check you can log it to the console

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

about 50

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

about 40

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

so that the code will only run if the requirements are met

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 (x = y) {
return ‘yay;
}

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

What are the three logical operators?

A

&&, ||, !

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

by using a logical operator && or | |

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

to run code over and over until the outcome is false or when it is capped

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

to see if the expression is true or not so it knows when to stop

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

a sequence of code run repeatidly

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

after the initialization expression

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

after the incrementation in the final expression or at the beginning of the iteration if it is the first time

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 starting a new iteration

64
Q

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

A

after the iteration of the loop

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;

66
Q

What does the ++ increment operator do?

A

goes up 1

67
Q

How do you iterate through the keys of an object?

A

using for in loops

68
Q

Why do we log things to the console?

A

it is a debugger, it helps inspect a certain element

69
Q

What is a “model”?

A

a model is the looks and design of a document that is being put on a page from memory. models are made with objects

70
Q

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

A

the document that is in the script element

71
Q

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

A

the nodes

72
Q

What is a DOM Tree?

A

a dom tree consists of multiple nodes each representing an text content, elements, attributes, etc.

73
Q

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

A

document.querySelector()
document.getElementById()

74
Q

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

A

document.querySelectorAll()

75
Q

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

A

it is easier to console log and console dir

76
Q

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

A

console.dir()

77
Q

Why would a

 tag need to be placed at the bottom of the HTML content instead of at the top?
A

because html loads top to bottom and you want everything to load before you can use dom elements

78
Q

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

A

a css selector and returns the first matching element

79
Q

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

A

a css selector and returns all matching elements

80
Q

What is the purpose of events and event handling?

A

it dictates an action that follow an event (user interaction)

81
Q

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

A

no

82
Q

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

A

element.addEventListener()

83
Q

What is a callback function?

A

a function passed into another function as an argument, which is then called inside the outer function to complete some kind of routine or action

84
Q

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

A

the event object

85
Q

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

A

a reference to the specific element in the document (whether its a class, id, input, etc). if we are not sure we can log it into the console, more info at mdn

86
Q

What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())

A

the second one will return the result of the function

87
Q

What is the className property of element objects?

A

it updates the class for the element

88
Q

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

A

you use the .className next to the variable that has the element selected

89
Q

What is the textContent property of element objects?

A

updates the text content of the element being selected

90
Q

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

A

you use .textContent next to the variable that has the element selected

91
Q

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

A

no, sometimes this will limit what you can do

92
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

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

A

‘submit’

97
Q

What does the event.preventDefault() method do?

A

prevents the default action

98
Q

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

A

dot notations of the properties

99
Q

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

A
100
Q

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

A

you could be making mistakes along the way without realizing then you have to change everything. wastes time

101
Q

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

A

no it does not

102
Q

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

A

appendChild()

103
Q

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

A

the attribute you want to set and the value

104
Q

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

A

createElement(), setAttribute(), appendChild()

105
Q

What is the textContent property of an element object for?

A

to update the text content of the object

106
Q

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

A

setAttribute() or className

107
Q

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

A

so that we can just call it multiple times easily

108
Q

What is the event.target?

A

it shows the event that is being targeted during the bubbling and capture phase

109
Q

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

A

because if we want to target a specific element within the parent element we can use event.target to do so (bubbling)

110
Q

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

A

event.target.tagName

111
Q

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

A

it takes a css selector and it returns the closest specified selector

112
Q

How can you remove an element from the DOM?

A

Element.remove()

113
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

create a div that holds everything inside and then select that div and add the event listener to it

114
Q

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

A

it removes the element from the page

115
Q

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

A

it takes a selector and it returns true or false

116
Q

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

A

getAttribute()

117
Q

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

A

after every step

118
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

querySelect each and every element?

119
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

a lot of if else statements

120
Q

What is JSON?

A

a text based data with object syntax

121
Q

What are serialization and deserialization?

A

converting the data into bytes so you can save it on a local drive. deserialization is the opposite

122
Q

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

A

JSON.stringify()

123
Q

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

A

JSON.parse()

124
Q

How do you store data in localStorage?

A

setItem()

125
Q

How do you retrieve data from localStorage?

A

getItem()

126
Q

What data type can localStorage save in the browser?

A

JSON strings

127
Q

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

A

when the window/document is about to be unloaded

128
Q

What is a method?

A

a method is a function which is the property of an object

129
Q

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

A

method definition is where you create your own custom methods, when you call a method you call it of the object
(object.method())

130
Q

Describe method definition syntax (structure).

A

var obj = {
property: function () {
return example;
}
}

131
Q

Describe method call syntax (structure).

A

object.method()

132
Q

How is a method different from any other function?

A

methods are associated with objects while functions are not

133
Q

What is the defining characteristic of Object-Oriented Programming?

A

objects can contain both data (as properties) and behavior (as methods)

134
Q

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

A

abstraction, encapsulation, inheritance, polymorphism

135
Q

What is “abstraction”?

A

being able to do complex things in simple ways

136
Q

What does API stand for?

A

application programming interface

137
Q

What is the purpose of an API?

A

an API simplifies programming by abstracting the underlying implementation and only exposing objects or actions the developer need

138
Q

What is this in JavaScript?

A

this is an implicit parameter of all Javascript functions

139
Q

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

A

that 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

140
Q

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

A

call time

141
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

it will be the window until we call it then it will be the variable character

142
Q

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

A

‘It’s a me, Mario!’

143
Q

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

A

undefined

144
Q

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

A

technically you can’t until you call it but we assume that it is the object that the this is in

145
Q

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

A

if it is in a function code block it is part of a function but if it is not then it is for a method call

146
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-base

147
Q

What is a prototype in JavaScript?

A

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

148
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

inheritance from the prototype chain

149
Q

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

A

the prototype

150
Q

What does the new operator do?

A

it creates a new instance for user-defined objects that has a constructor function

151
Q

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

A

prototype property

152
Q

What does the instanceof operator do?

A

it checks to see if the prototype property of the constructor matches with the prototype chain of an object and it returns a boolean

153
Q

What is a “callback” function?

A

a function that goings in another function as an argument

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

155
Q

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

A

adding to the setInterval() as a callback function

156
Q

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

A

0 so it would execute immediately

157
Q

What do setTimeout() and setInterval() return?

A

it returns an id (timeoutID and intervalID) so that it can be identified and cleared using clearInterval() or clearTimeout()