JavaScript Flashcards

1
Q

What is the purpose of variables?

A

the purpose of variables is to store data for future use

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 keyword “var” and an assignment operator and 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

with 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

letters, numbers, $, and _

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

var score
and
var Score

are different

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 number data

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 be a light switch
it stores “true” or “false”
and is the logic in JavaScript

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 the assignment operator and assigns values to variables

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

with 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 an object and intentional. a placeholder

undefined is automatically assigned to a variable

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 add context to what you’re doing. also makes the data easier to read

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

Give five examples of JavaScript primitives

A
null
undefined
string
number
boolean

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 a string concatenation?

A

It’s the combination of two strings by 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 + operator serve in JavaScript?

A

it’s used as addition in arithmetic, as concatenation with strings and variables

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 whatever value is specified to the variable and then reassigns it 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

They’re used to store related data in one place

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

What are object properties?

A

Object properties serve as the “variables” of an object

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

Describe object literal notation

A

a list of key value pairs within an object

a key is assigned a value and each pair is separated by a comma

var = {
key: value,
secondKey: value
};

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

with the “delete” keyword

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

dot notation or bracket notation

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

What are variables called in an object?

A

they are called properties

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

What are the names of properties and methods called in an object?

A

they’re called keys

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

What are arrays used for?

A

arrays are used when storing a list of data that’s all related to each other and especially when you’re unsure of how many items you’ll be storing

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

Describe array literal notation

A

var array = [‘value1’, ‘value2’, ‘value3’];

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

How are arrays different from “plain” objects?

A

Instead of properties, they have indexes

Arrays are ordered, objects aren’t

objects ‘values’ are given property names

arrays aren’t.. they’re just indexed

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

What is the length property of an array?

A

It tells us how long an array is

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

How do you calculate the last index of an array?

A

array.length - 1

since the first index is array[0]

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

What is a function in JavaScript?

A

repeatable, named block of code that does

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

Describe the parts of a function definition

A
function keyword
optional function name
names for parameter list
{
return some work;}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Describe the parts of a function call

A
function keyword
optional name
optional parameter
opening brace of function code block
code
optional return statement
closing brace of function code block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

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

A

definition has a code block
call does not

definition passes parameters
call (passes arguments)

definition has function keyword
call does not

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

What’s the difference between a parameter and an argument?

A

parameters are part of a function definition

arguments are part of a function call

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

Why are function parameters useful?

A

to add context and to pass info

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

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

A
  1. ) it returns a value

2. ) it stops the function

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

Why do we log things to the console?

A

to double check our work and make sure that everything is working as intended

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

What is a method?

A

a method is a function within an object
just like how variables become properties within a function

a function stored within the property of an object

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

How is a method different from any other function?

A

a function is called by name and passed parameters

a method is called along with a object using dot-notation and can access the data in said object

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

How do you remove the last element from an array?

A

with the pop() method

array.pop()

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

How do you round a number to the nearest integer?

A

with the Math.floor() method

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

How do you generate a random number?

A

with the Math.random() method

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

How do you delete an element from an array?

A

with the splice() method

array.splice()

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

How do you append an element to an array?

A

with the push() method

array.push()

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

How do you break a string up into an array?

A

with the split() method

string.split()

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

Do string methods change the original string?

A

no, but if i wasn’t sure, i’d log to the console

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

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

A

40-ish

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

Roughly how many array methods are there according to MDN?

A

30 ish

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

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

M
D
N

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

Give 6 examples of comparison operators

A
less than <
less than or equal to <=
greater than >
greater than or equal to >=
is equal to ==
strictly equal to ===
is not equal to !==
is strictly not equal to !===
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What data type do comparison expressions evaluate to?

A

booleans

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

What is the purpose of an if statement?

A

to execute an action depending on if a condition is met

allows the computer to make decisions

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

Describe the syntax (structure) of an if statement

A

if statement
condition (x > y)

IF condition is met, then one or more expressions is executed

ELSE a different expression is executed

return

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

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

A

by using a logical operator

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

What is the purpose of a loop?

A

to repeat a block of code many times

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

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

A

to let the loop know when to stop

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

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

A

a single time that the code block of the loop runs

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

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

A

before the code is ran

before each iteration

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

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

A

before the condition

before the loop runs

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

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

A

after the initialization

and before each iteration

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

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

A

after each iteration

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

What does the ++ increment operator do?

A

it increments a var by 1

and gets us closer to ending the loop

68
Q

How do you iterate through the keys of an object?

A

for… in loop

69
Q

Why do we log things to the console?

A

to make sure things are loading and working correctly.

to check work.

70
Q

What is a “model”

A

It’s a smaller representation of something

71
Q

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

A

The HTML document

72
Q

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

A

it’s referring to the JavaScript data types

73
Q

What is a DOM tree?

A

a representative chunk of the page built as JavaScript objects

74
Q

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

A

.querySelectorAll

75
Q

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

A

if you wanted to re-use the value, it’s best to assign it to a variable

76
Q

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

A

if you wanted to re-use the value, it’s best to assign it to a variable so that you won’t have to query the DOM again

77
Q

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

A

because all of the HTML needs to load first

78
Q

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

A

it takes an HTML element/class/ID as a string as an argument and returns the first instance of that

79
Q

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

A

it takes a CSS selector as a string as an argument and returns the first instance of that element

80
Q

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

A

it takes a CSS selector as a string and returns a static NodeList of all of the elements that match the specified group of selectors

81
Q

Why do we log things to the console?

A

to check our work and make things are working as intended

82
Q

What is the purpose of events and event handling?

A

The purpose of events is to run a script when an event is fired. Event handling is important for making a web page more interactive

83
Q

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

A

no!

84
Q

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

A

the .addEventListener method

85
Q

What is a callback function?

A

A function called within a function

86
Q

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

A

an event object

87
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 tells us what element the eventlistener is listening on. if i was unsure, i would log it to the console or look it up on MDN ;)

where the event is actually originating from

88
Q

What is the difference between these two snippets of code?

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

A

the first snippet is waiting until the click event is fired to call the handleClick function

the second is calling the handleClick function as soon as the page loads

89
Q

What is the className property of element objects?

A

it gives us and allows us to change the className of an element

90
Q

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

A

you would use the className property of the object who’s class you’re trying to change and assign that a string value of what you want the className to be

91
Q

What is the textContent property of element objects?

A

It tells us and allows up to change the text content of a given object

92
Q

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

A

you would use the textContent property of the object who’s textContent you’re trying to change and assign that a string value of whatever you want the new text to be

93
Q

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

A

YES!

94
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

way more complicated

95
Q

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

A

it makes the information reusable and allows us to track and update values

96
Q

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

A

the focus event is fired

97
Q

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

A

the blur event is fired

98
Q

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

A

the input event is fired

99
Q

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

A

the submit event is fired

100
Q

What does the event.preventDefault() method do?

A

it tells the browser that if the event does not get “explicitly handled”, then it’s default action should not be taken

101
Q

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

A

the “.elements” property of forms contains all of the form’s controls

102
Q

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

A

the “.value” property

103
Q

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

A

writing all of that code just to realize it doesn’t work would be a colossal waste of time and much harder to diagnose and fix where it broke

104
Q

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

A

you can see your code working (or not working) in real time.

makes it very easy to spot errors

105
Q

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

A

no! it just creates the element node in the DOM

106
Q

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

A

you use the appendChild() method on the parent element and pass the child as an argument

107
Q

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

A

two strings

the first one is the attribute name and the second one is the value of the attribute

(‘attribute’, ‘value’)

108
Q

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

A

createElement()

appendChild()

109
Q

What is the textContent property of an element object for?

A

the textContent property allows us to get the textContent of a node

it also allows us to set the textContent of a node

110
Q

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

A

node. textContent gets the value

nod. textContetnt = ‘Sets text content’;

111
Q

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

A

saves a LOT of time if many elements are going to be created when an event is fired

112
Q

What is the event.target?

A

the element where the event originates from

113
Q

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

A

because of event bubbling

114
Q

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

A

.tagName

115
Q

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

A

it takes in a CSS selector and returns the closest ancestor that matches the selector

116
Q

How can you remove an element from the DOM?

A

you can call the .remove() method on whichever element you want to remove…

WITH NO ARGUMENT

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

you can take advantage of event delegation and more specifically, bubbling, and just add an event listener to it’s parent element

can use tagName to make sure it matches

118
Q

What is event.target?

A

where the event originated from

119
Q

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

A

It’s removed from the document flow entirely

120
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 value

121
Q

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

A

assign it to a value

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 each element

123
Q

What is JSON?

A

JavaScript Object Notation

allows us to transmit objects

124
Q

Why are serialization and deserialization useful?

A

TO MAKE IT POSSIBLE TO SEND DATA ELSEWHERE

and to turn that data into objects

125
Q

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

A

JSON.stringify()

126
Q

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

A

JSON.parse()

127
Q

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

A

JSON.parse()

128
Q

How do you store data in localStorage?

A

you use the setItem() method on the localStorage object and pass two arguments

the first is the name of your storage and the second will be a JSON array

129
Q

How do you retrieve data from localStorage?

A

you would use the getItem() of the localStorage object and pass an argument that is the key of the data you’re trying to retrieve. you can then parse it

130
Q

What data type can localStorage save in the browser?

A

strings !

131
Q

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

A

Right before the page unloads all of its data

such as when the page is closed or refreshed

132
Q

What is a method?

A

A method is a function that is a property of an object

133
Q

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

A

a method definition is done within an object

a method call is called on an object with dot notation

134
Q

Describe method definition syntax (structure)

A

a method definition is done within an object
we define it as we would with a property, starting with a :
followed by the function keyword, name, parameters, and then code block

135
Q

Describe method call syntax (structure)

A

a method is called on an object with dot notation and optional arguments

136
Q

How is a method different from any other function?

A

a method is defined within an object and only called on objects

137
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

138
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

139
Q

What is abstraction?

A

It’s doing potentially complex things in a simpler way.

140
Q

What does API stand for?

A

Application Programming Interface

141
Q

What is the purpose of an API?

A

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

142
Q

What is “this” in JavaScript?

A

“This” is an implicit parameter in JavaScript that is given its value based on when it’s called. By default, it’s value is window

143
Q

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

A

It means that it can be used within a function without ever being explicitly defined.

144
Q

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

A

It’s determined at call time

145
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’s undefined

146
Q

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

A

it should return, ‘It’s-a-me, Mario!’

because the value of “this” in this function is the character object

147
Q

Given the above character object, what is the result of the following code snippet? Why?

var hello = character.greet;
hello();
A

it should return, “It’s-a-me, undefined!”

this’ value changed when it was reassigned to the hello variable.

hello doesn’t have a firstName property

148
Q

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

A

for a function or method definition, the value of “this” will always be it’s default value, window

149
Q

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

A

The value of “this” would become whichever object it was being used within.

150
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype-based inheritance

or Prototypal inheritance

151
Q

What is a prototype in JavaScript?

A

A prototype is a very broad object that descendant objects can delegate to and use the functions of

152
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

because each of those are able to refer to a larger, prototype class that contains all of those methods.

prototypal inheritance

153
Q

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

A

JavaScript would search in that object’s prototype for a property/method

154
Q

What does the new operator do?

A

it let’s us create an instance of an object of a constructor function

  1. ) creates a blank, object
  2. ) sets prototype to the prototype of the constructor
  3. ) assigns this to the prototype
155
Q

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

A

prototype

156
Q

What does the instanceof operator do?

A

it tells us if the prototype property appears anywhere in the prototype chain of an object. if it is a descendant.
returns a boolean

157
Q

What is a callback function’?

A

a function defined within another function

158
Q

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

A

setInterval(function, delay, arg1, arg2, arg n)

159
Q

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

A

setInterval(function, delay, arg1, arg2, arg n)

160
Q

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

A

0ms

161
Q

What is AJAX?

A

AJAX is an asynchronous process that allows the client to make requests while the page is loading. When the server responds with data, an event is fired.

162
Q

What does AJAX stand for ?

A

Asynchronous Javascript and XML

163
Q

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

A

the XMLHttpRequest() constructor

164
Q

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

A

the ‘load’ event

165
Q

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

A

What is prototypes???