JavaScript Flashcards

1
Q

What is the purpose of variables?

A

data storage

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

variable name = variable 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, numbers, $, underscore

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

What does it mean to say that variable names are “case sensitive”?

A

two variables can share the same name but have different capitalization, making them different variables (don’t do this; it’s bad practice)

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

storage of text data

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

storage of numeric data types

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 make decisions

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

assignment operator

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

How do you update the value of a variable?

A

variable name = 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: intentional absence created by a human

- undefined: lack of value recognized by JavaScript

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

for clarity and to describe the variable or value 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

numeric

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

What is string concatenation?

A

adding two or more strings together to create a new string

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

arithmetic, 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 another value to variable and the result of that expression gets assigned 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 group together a set of 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

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

Describe object literal notation

A

var object = {

};

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

by 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

dot notation or 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

store 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

var name = [ ];

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

only uses numeric index, contains a length property that is constantly updated, multiple methods to interact with arrays

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

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

stores (true) count of number of items are in 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

subtract 1 from the variable

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

What is a function in JavaScript?

A

a set of code block for performing a task that is reusable

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, name, parameters list, {code block};

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

there can be 0 to many arguments

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

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

A
function call: there is a function name followed by ();
- executes code

function definition: there is a function keyword and a code block

  • code does not run
  • gives function a name
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

parameter: placeholder for argument value
- value is unknown until function gets called and argument(s) is passed

argument: a known value that gets passed when a function 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
  • serves as a placeholder for arguments
  • allows for varying results based on arguments that will get passed later on
  • reusable behavior
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
  • causes function to produce a value that can be used in programming
  • prevents any more code in function code block from being 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

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 which is a property of an object
  • in JavaScript functions themselves are objects, so, in that context, a method is actually an object reference to a function
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 part of 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()

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

strings are immutable; call a method to check the values

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

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, it varies by situation

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

50+

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
== equal to
=== strict equal to
!= not equal to
>
>=
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

allows for decision-making and different pathways for code

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 (condition) {
statement
};

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

What are the three logical operators?

A

&& logical and
|| logical or
! logical not

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

(expression) && (expression)

expression) || (expression

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 check a condition repeatedly until it returns false

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

tells the loop 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

how ever many times the code block runs

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

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

A

before each iteration

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

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

A

once, before the loop begins

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

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

A

after initialization and before each loop iteration

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

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

A

after code block runs and before the condition runs again

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

Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

break keyword

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

What does the ++ increment operator do?

A

adds one, returning a value that substitutes the current value

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

How do you iterate through the keys of an object?

A

using for…in loop

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

Why do we log things to the console?

A

to look at the outputs we’re working with and to also debug when a problem occurs

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

What is a “model?”

A

a replica of something

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

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

A

HTML document

71
Q

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

A

data type object

72
Q

What is a DOM tree?

A

a representation of the objects in HTML, including the parent element, child element, nodes

73
Q

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

A

.querySelector(‘css selector’)

.getElementById(‘id’)

74
Q

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

A

.querySelectorAll(‘css selector’)

75
Q

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

A

to access it again later (also for efficiency)

76
Q

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

A

console.dir

dir = directory

77
Q

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

A

HTML document loads from top to bottom so the browser needs to parse all elements before JavaScript code can access it

78
Q

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

A

CSS selector; the first matching element

79
Q

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

A

CSS selector; all matching elements (nodelist)

80
Q

What is the purpose of events and event handling?

A

creates interactivity with the user; code responds to events triggered by the user

81
Q

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

A

no because there could be a function with no parameters

82
Q

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

A

.addEventListener

83
Q

What is a callback function?

A

a function passed into another function as an argument, which is then invoked inside outer function to complete an action

84
Q

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

A

an object with all data about the event that just occurred

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

property of the event object and the element where the event occurred; check MDN

86
Q

What is the difference between these two snippets of code?

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

A
  • the first snippet is a function definition
  • the second snippet is a function call
  • the event handler will be undefined because the function call gets replaced with a return
  • there is never a return for an event handler function
87
Q

What is the className property of element objects?

A

property that sets the value of the class attribute of the specified element

88
Q

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

A

query for the element, get new value, re-assign value to element using .className

89
Q

What is the textContent property of element objects?

A

text content of specified node and its descendants

90
Q

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

A

query for the element, get new value, re-assign value to element using .textContent

91
Q

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

A

no; only needed when you need to know where the event occurred

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 because you would need to retrieve text, convert it to a number, then return it
  • variables are easier to work with and easy to identify
93
Q

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

A
  • more efficient and is easily accessible by JavaScript

- data should be stored in JavaScript to keep track of values

94
Q

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

A

focus

95
Q

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

A

blur

96
Q

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

A

input

97
Q

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

A

submit

98
Q

What does the event.preventDefault() method do?

A

prevents default behavior from happening

e.g., browser automatically reloading page with form values in URL

99
Q

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

A

it reloads the page with form values

100
Q

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

A

.elements

elements property

101
Q

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

A

.value

value property

102
Q

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

A

not knowing where a problem occurred and its effect on other lines of code

103
Q

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

A

see code in action, be able to spot problems and fix them as needed

104
Q

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

A

it creates an element node, but it’s on the page or visible yet

105
Q

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

A

.appendChild

append = to add to the end

106
Q

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

A

(name, value)

107
Q

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

A
  1. create element
  2. give it content
  3. query DOM for target parent
  4. add to DOM by appending child to parent
108
Q

What is the textContent property of an element object for?

A
  • text content of the node and its descendants

- it can be used to retrieve and set text content of an element

109
Q

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

A

.setAttribute

.querySelector for the element then assign string to class name property

110
Q

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

A
  • can test the function to see what it returns when an argument is passed
  • can reuse the code later
111
Q

What is the event.target?

A

it returns the element that was triggered by an event

112
Q

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

A

event bubbling

113
Q

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

A

.tagName

114
Q

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

A

string selector; closest ancestor element

115
Q

How can you remove an element from the DOM?

A

.remove()

116
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

wrap it on a parent element then add event listener on that parent element

117
Q

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

A

it will disappear and be removed from document flow

118
Q

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

A

selector string; boolean

119
Q

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

A

.getAttribute()

120
Q

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

A

all steps

121
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
  • create custom individual event handler for each tab

- use .querySelector() instead of .querySelectorAll()

122
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

add a new if statement for each tab

123
Q

What is JSON?

A
  • JavaScript Object Notation

- text-based data format that exists as a string, following JavaScript object syntax

124
Q

What are serialization and deserialization?

A
  • serialization: converting object to a string/series of bytes
  • deserialization: converting string/stream of bytes to a native object
125
Q

Why are serialization and deserialization useful?

A

useful for when you want to transmit data across a network or need data to be stored on a disk

126
Q

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

A

JSON.stringify()

127
Q

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

A

JSON.parse()

128
Q

How to you store data in localStorage?

A

.setItem(key, value)

129
Q

How to you retrieve data from localStorage?

A

storage.getItem(keyName)

if empty, it will return null

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

when the window is about to close or refresh

132
Q

What is a method?

A

a function which is a property of an object

133
Q

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

A
  • method definition: contains a function keyword, code block, and is being assigned to a property
  • method call: only contains name of object followed by the method ()
134
Q

Describe method definition syntax (structure).

A
property name : function (parameters) {
//code block;
};
135
Q

Describe method call syntax (structure).

A

object.method();

136
Q

How is a method different from any other function?

A

it’s a property of an object

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

simplifying complex concepts

140
Q

What does API stand for?

A

Application Programming Interface

141
Q

What is the purpose of an API?

A

software abstraction; simplifying complex behavior so people can use it without having to fully understand how something actually works

142
Q

What is this in JavaScript?

A

an implicit parameter of all JavaScript functions

143
Q

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

A

always present in function’s code block even though it was never included in the parameter list or declared with a variable

144
Q

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

A

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
  • nothing; there is no “this” yet

- method must be called first

146
Q

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

character.greet();

Why?

var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
A

It’s-a-me, Mario!

  • the greet method is being called on the character object
147
Q

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

var hello = character.greet;
hello();

Why?

var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
A

It’s-a-me, undefined!

  • “this” is the window which 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

you can’t bc it doesn’t have a value until it is called

149
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 when the function is called

150
Q

What kind of inheritance does the JavaScript programming language use?

A

prototypal inheritance

151
Q

What is a prototype in JavaScript?

A

a model of an object that other objects can use to build upon

152
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

by using the prototype object, which contains properties and methods that can be used by other objects

153
Q

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

A

it borrows from the prototype object

154
Q

What does the new operator do?

A
  1. creates a blank, plain JavaScript object
  2. adds a property to the new object (__proto__) that links to the constructor function’s prototype object
  3. binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step)
  4. returns this if the function doesn’t return an object
155
Q

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

A

prototype property

156
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 (return value is a boolean)

157
Q

What is a “callback” function?

A

a function that gets passed into another function as an argument

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

159
Q

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

A

setInterval() function

160
Q

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

A

zero

161
Q

What do setTimeout() and setInterval() return?

A

a numeric, non-zero value (ID) which identifies the timer created

162
Q

What is a client?

A

a piece of software that accesses a service made available by a server as part of the client-server model

163
Q

What is a server?

A

a computer hardware or software that provides functionality for other programs or devices known as clients

(wait for a request from client and provide what was requested)

164
Q

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

A

GET

165
Q

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

A
  1. an HTTP method that describes the action to be performed
  2. the request target, usually a URL or absolute path of the protocol, port, and domain
  3. the HTTP version, which defines the structure of the remaining message, acting as an indicator of the expected version to use for the response
166
Q

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

A
  1. the protocol version, usually HTTP/1.1
  2. a status code (success or failure) of the request
  3. a status text providing description of the status code to help person understand the HTTP message
167
Q

What are HTTP headers?

A
  • an HTTP header consists of its case-insensitive name followed by a colon (:), then by its value

HTTP headers…

  • let the client and the server pass additional information with an HTTP request or response
  • allow you to provide additional metadata about the request or response
168
Q

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

A

MDN

169
Q

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

A
  • a body is optional

- applicable in certain cases like 201, which indicates that data was successfully created

170
Q

What is AJAX?

A

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

171
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

172
Q

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

A

XMLHttpRequest object

173
Q

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

A

load

174
Q

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

A

prototypal inheritance