JavaScript Flashcards

1
Q

What is the purpose of variables?

A

store information for use later

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

How do you declare a variable?

A

var keyword eg var fullName

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

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

upper case and lower case are treated independntly. eg car is not the same as Car

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

represent 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

represent numeric data type

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

represent 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

assignment eg var name = “jon”

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)

note that keyword var is not needed

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

undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.

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

clarity in the output - makes it easier to identify.

A console log “label” is simply a short string that describes 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, undefined, null – bigint, symbol

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

joining together strings

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

What purpose(s) does the + plus operator serve in JavaScript?

A

addition 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

addition assignment, adds value and assigns the result to the variable

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

What are objects used for?

A

group together a set of properties and methods

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

What are object properties?

A

tell us about the object

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

Describe object literal notation.

A
similar to css rulesets.
var hotel = {
name: "quay"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you remove a property from an object?

A

delete keyword followed by the property name and object name

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
object.property

bracket notation
object{‘property’]

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

What are arrays used for?

A

storing a list of values that are related to each other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
colors = ['red', 'white', 'blue']
26
How are arrays different from "plain" objects?
the key for a value in an array is its index number order matters in an array arrays have length property each piece of data is not named
27
What number represents the first index of an array?
0
28
What is the length property of an array?
holds the number of items in an array
29
How do you calculate the last index of an array?
array.length - 1
30
What is a function in JavaScript?
a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output
31
Describe the parts of a function definition.
``` function keyword, name of the function, function parameters, javascript statements in curly braces ```
32
Describe the parts of a function call.
name of the function, parenthesis with parameters
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call does not need the function keyword and includes the values for the parameters
34
What is the difference between a parameter and an argument?
parmeter - placeholder for an argument, value is not known until argument is passed. argument- actual values passed to the function in place of the parameter
35
Why are function parameters useful?
they act as placeholders
36
What two effects does a return statement have on the behavior of a function?
causes the function to produce a value we can use in our program exits the code block
37
Why do we log things to the console?
so we can see the output (debugging tool)
38
What is a method?
a function as a property inside of an object
39
How is a method different from any other function?
method is associated with an object
40
How do you remove the last element from an array?
with the pop method
41
How do you round a number down to the nearest integer?
with the floor method
42
How do you generate a random number?
random method of the math object
43
How do you delete an element from an array?
splice method
44
How do you append an element to an array?
push method
45
How do you break a string up into an array?
split method
46
Do string methods change the original string? How would you check if you weren't sure?
No, | console log the original string
47
Is the return value of a function or method useful in every situation?
no. eg push will return length of new string
48
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
mdn
49
Give 6 examples of comparison operators.
``` not equal to (!=) strict equal to (===) >< >= <= ```
50
What data type do comparison expressions evaluate to?
boolean, true or false
51
What is the purpose of an if statement?
evaluates a condition, if condition is true then the code block is executed
52
Is else required in order to use an if statement?
no
53
Describe the syntax (structure) of an if statement.
keyword, condition, opening curly brace if (condition >= condition) { }
54
What are the three logical operators?
``` logical and ( && ) logical or ( | | ) logical not ( ! ) ```
55
How do you compare two different expressions in the same condition?
logical and or logical or operator eg ((5<2) && (2>=3))
56
What is the purpose of a loop?
repeat a condition until the loop ends
57
What is the purpose of a condition expression in a loop?
loop will continue until the condition reaches a specific state or number
58
What does "iteration" mean in the context of loops?
running the loop once
59
When does the condition expression of a while loop get evaluated?
before executing the statement
60
When does the initialization expression of a for loop get evaluated?
once at the start
61
When does the condition expression of a for loop get evaluated?
before each loop iteration
62
When does the final expression of a for loop get evaluated?
after each iteration
63
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
64
What does the ++ increment operator do?
it increments its operand and returns a value
65
How do you iterate through the keys of an object?
with a for-in loop
66
Why do we log things to the console?
so we can see the output
67
What is a "model"?
a representation of something else
68
Which "document" is being referred to in the phrase Document Object Model?
html document
69
What is the word "object" referring to in the phrase Document Object Model?
data type object in javascript All of the properties, methods, and events available for manipulating and creating web pages are organized into objects.
70
What is a DOM Tree?
an element plus all of its children
71
Give two examples of document methods that retrieve a single element from the DOM.
getElementsById('id') | querySelector('css selector')
72
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementsByClassName('class')
73
Why might you want to assign the return value of a DOM query to a variable?
it saves the browser looking through the DOM tree to find the same elements again
74
What console method allows you to inspect the properties of a DOM element object?
console.dir()
75
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so that the html can load before the javascript
76
What does document.querySelector() take as its argument and what does it return?
it takes a css selector and returns the first matching element
77
What does document.querySelectorAll() take as its argument and what does it return?
it takes a css selector and returns all that match
78
What is the purpose of events and event handling?
create user interactivity | events - things that happen (could be user interactivity)
79
Are all possible parameters required to use a JavaScript method or function?
no
80
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
81
What is a callback function?
a function passed as an argument into another function, which is then invoked inside the outer function to complete some kind of routine or action
82
What object is passed into an event listener callback when the event fires?
event object
83
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
the selected node, you can console log or console dir
84
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
the top is a reference to handleClick while the bottom is being executed immediately with no parameters
85
What is the className property of element objects?
className getsor sets the value of the class attribute
86
How do you update the CSS class attribute of an element using JavaScript?
element.classname and assignment operator
87
What is the textContent property of element objects?
represents the text content of the node and its descendents
88
How do you update the text within an element using JavaScript?
element.textContent = text content
89
Is the event parameter of an event listener callback always useful?
no
90
Would this assignment be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
more complicated. html only has string data type and would need to be converted
91
Why is storing information about a program in variables better than only storing it in the DOM?
our program cannot be changed by the user changing text content
92
What event is fired when a user places their cursor in a form control?
focus
93
What event is fired when a user's cursor leaves a form control?
blur
94
What event is fired as a user changes the value of a form control?
input
95
What event is fired when a user clicks the "submit" button within a form
submit event
96
What does the event.preventDefault() method do?
prevents the events default behavior
97
What does submitting a form without event.preventDefault() do?
reloads teh page
98
What property of a form element object contains all of the form's controls.
elements
99
What property of a form control object gets and sets its value?
value
100
What is one risk of writing a lot of code without checking to see if it works so far?
hard to see where it went wrong
101
What is an advantage of having your console open when writing a JavaScript program?
if something happens the console will throw an error
102
Does the document.createElement() method insert a new element into the page?
no it creates a new element
103
How do you add an element as a child to another element?
with the appendchild method
104
What do you pass as the arguments to the element.setAttribute() method?
class and value of the class
105
What steps do you need to take in order to insert a new element into the page?
create the new element, add any text content/attributes, then append it to another element
106
What is the textContent property of an element object for?
represents the text content of the node
107
Name two ways to set the class attribute of a DOM element.
className property and setAttribute, classlist
108
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
it can be reused it is faster than creating a new one every time can be updated easily give name to a process
109
What is the event.target?
target property of the event. a reference to the object onto which the event was displached
110
Why is it possible to listen for events on one element that actually happen its descendent elements?
because of event bubbling
111
What DOM element property tells you what type of element it is?
tagname
112
What does the element.closest() method take as its argument and what does it return?
``` it takes a css selector The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. ```
113
How can you remove an element from the DOM?
using the remove() method
114
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?
add it to an ancestor element
115
What is the affect of setting an element to display: none?
hides that element from the page
116
What does the element.matches() method take as an argument and what does it return?
it takes a selector string and returns a boolean
117
How can you retrieve the value of an element's attribute?
getAttribute method
118
At what steps of the solution would it be helpful to log things to the console?
every step
119
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?
new event handler for every single one
120
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?
an if/else statement for each data view
121
What is JSON?
JSON is a text-based data format following JavaScript object syntax useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data.
122
What are serialization and deserialization?
Serialization is a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage. Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.
123
Why are serialization and deserialization useful?
you can save the state of an object and recreate it when needed
124
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify
125
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse
126
How do you store data in localStorage?
with the .setitem() method
127
How do you retrieve data from localStorage?
the getItem() method
128
What data type can localStorage save in the browser?
strings
129
When does the 'beforeunload' event fire on the window object?
when the window, the document, and its resources are about to be unloaded.
130
What is a method?
A method is a function which is a property of an object.
131
How can you tell the difference between a method definition and a method call?
method call: calculator.add() method definition: add: function (x, y) { return x + y; },
132
Describe method definition syntax (structure).
``` method name : function keyword, (parameters) { function } ```
133
Describe method call syntax (structure).
object.method name (parameters)
134
How is a method different from any other function?
a method is within an object
135
What is the defining characteristic of Object-Oriented Programming?
objects can hold data and behavior
136
What are the four "principles" of Object-Oriented Programming?
abstraction - being able to work with complex things in simple ways Encapsulation - bundling of data Inheritance - basing an object or class upon another object or class Polymorphism - single interface to entities of different types
137
What is "abstraction"?
abstraction - being able to work with complex things in simple ways
138
What does API stand for?
application programming interface
139
What is the purpose of an API?
an application programming interface connects computers or pieces of software to each other
140
What is this in JavaScript?
this is an implicit parameter of all JavaScript functions. contains a reference to the object
141
What does it mean to say that this is an "implicit parameter"?
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
142
When is the value of this determined in a function; call time or definition time?
call time
143
``` 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); } }; ```
nothing
144
given the above character object, what is the result of the following code snippet? Why
It's-a-me, Mario!
145
``` Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello(); ```
its a me undefined
146
How can you tell what the value of this will be for a particular function or method definition?
cant tell, would be nothing
147
How can you tell what the value of this is for a particular function or method call?
Find where the function is called and look for an object to the left of the dot
148
What kind of inheritance does the JavaScript programming language use?
prototypal
149
What is a prototype in JavaScript?
an object that holds properties or methods
150
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?
the methods are defined on a prototype object and the arrays, strings, and numbers borrow those methods
151
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
the prototype
152
What does the new operator do?
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.
153
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
154
What does the instanceof operator do?
he instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
155
What is a "callback" function?
a function passed into another function as an argument
156
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?
settimeout, setinterval
157
How can you set up a function to be called repeatedly without using a loop?
setinterval
158
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
159
What do setTimeout() and setInterval() return?
an interval ID that can be used by clearinterval and cleartimeout
160
What is a client?
service requesters
161
what is a server
providers of a resource or service
162
Which HTTP method does a browser issue to a web server when you visit a URL?
get
163
What three things are on the start-line of an HTTP request message?
method, request target, http version
164
What three things are on the start-line of an HTTP response message?
protocol version, status code, status text
165
What are HTTP headers?
line of code. The HTTP headers are used to pass additional information between the clients and the server through the request and response header
166
Where would you go if you wanted to learn more about a specific HTTP Header?
mdn
167
Is a body required for a valid HTTP request or response message?
no
168
What is AJAX?
Ajax is a technique for loading data into part of a page | without having to refresh the entire page
169
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
170
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
171
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
the load event (onload)
172
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototypal inheritance
173
What is a code block? What are some examples of a code block?
code in curly braces, function code block
174
What does block scope mean?
it refers to the area that a block has jurisdiction
175
What is the scope of a variable declared with const or let?
block scope
176
What is the difference between let and const?
let is mutable while const is not
177
Why is it possible to .push() a new value into a const variable that points to an Array?
The const keyword creates a read-only reference to a value. The readonly reference cannot be reassigned but the value can be change.
178
How should you decide on which type of declaration to use?
if you need the value to change, use let | if you dont nee it to change , use const
179
What is the syntax for writing a template literal?
let var = surround string you want in backticks and use ${variable} for the variable you want inserted
180
What is "string interpolation"?
when JS automatically replaces variables and expressions with their values
181
What is destructuring, conceptually?
taking something apart
182
What is the syntax for Object destructuring?
const/let {object property name: variable name} = object
183
What is the syntax for Array destructuring?
const/let [variable name, variable name, variable name] = array
184
How can you tell the difference between destructuring and creating Object/Array literals?
which side the array/object name is on
185
What is the syntax for defining an arrow function?
let add = (x, y) => x + y;
186
When an arrow function's body is left without curly braces, what changes in its functionality?
the result is automatically returned
187
How is the value of this determined within an arrow function?
an arrow function captures the this value of the enclosing context instead of creating its own this context this is called on definition
188
What is a CLI?
command line interface
189
What is a GUI?
graphical user interface, interaction through graphical icons
190
Give at least one use case for each of the commands listed in this exercise. ``` man cat ls pwd echo touch mkdir mv rm cp ```
``` man - looking up manual cat - combining text files ls - ensuring that you created the files you intended to pwd - you can tell what directory you are currently on echo - touch - create an empty file mkdir - create directories mv - rename directory rm - delete a directory cp - copy a directory ```
191
What are the three virtues of a great programmer?
laziness, impatience, and hubris
192
What is Node.js?
Node.js is a program that allows JavaScript to be run outside of a web browser
193
What can Node.js be used for?
It is commonly used to build back ends for Web applications, command-line programs, or any kind of automation that developers wish to perform.
194
What is a REPL?
read eval print loop a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise
195
When was Node.js created?
2009
196
What back end languages have you heard of?
Ruby, PHP, Java, . Net, and Python
197
What is the process object in a Node.js program?
The process object is a global that provides information about, and control over, the current Node.js process.
198
How do you access the process object in a Node.js program?
process keyword
199
What is the data type of process.argv in Node.js?
array of strings
200
What is a JavaScript module?
a single js file
201
What values are passed into a Node.js module's local scope?
exports , require , module , __filename , __dirname
202
Give two examples of truly global variables in a Node.js program.
console | process
203
What is the purpose of module.exports in a Node.js module?
allow exporting of a module to be used somewhere else
204
How do you import functionality into a Node.js module from another Node.js module?
const add = require('./add');
205
What is the JavaScript Event Loop?
a programming construct or design pattern that waits for and dispatches events or messages in a program.
206
What is different between "blocking" and "non-blocking" with respect to how code is executed?
Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. all synchronous code is blocking
207
What module does Node.js include for manipulating the file system?
fs
208
What method is available in the Node.js fs module for writing data to a file?
fs.writefile
209
Are file operations using the fs module synchronous or asynchronous?
both
210
What is NPM?
npm is a package manager for the JavaScript programming language website, cli, registry
211
What is a package?
A package is a file or directory that is described by a package.json file
212
How can you create a package.json with npm?
npm init
213
What is a dependency and how to you add one to a package?
Packages required by your application in production | npm install name of
214
What happens when you add a dependency to a package with npm?
npm downloads the dependency node module gets created dependency gets put in node-module library gets added to json
215
How do you add express to your package dependencies?
npm install express
216
What Express application method starts the server and binds it to a network PORT?
listen method
217
How do you mount a middleware with an Express application?
app.use
218
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
req, res
219
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
220
What does the express.json() middleware do and when would you need it?
parses incoming json, need it when receiving json body messages
221
What is the significance of an HTTP request's method?
to indicate the desired action to be performed for a given resource
222
What is PostgreSQL and what are some alternative relational databases?
PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS). It is often cited as the most advanced open source database of its kind and is well-liked by the developer community for its robust feature set, standards compliance, and reliability. Other popular relational databases include MySQL (also free), SQL Server by Microsoft, and Oracle by Oracle Corporation.
223
What are some advantages of learning a relational database?
if you are storing related data, then a relational database is probably a good first choice. A quality of many relational databases is that they support good guarantees about data integrity. Relational databases are arguably the most widely used kind of database.
224
What is one way to see if PostgreSQL is running?
sudo service postgresql status
225
What is a database schema?
collection of tables A schema defines how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.
226
What is a table?
A table is a list of rows each having the same set of attributes. For example, all students in a "students" table could have "firstName", "lastName", and "dateOfBirth" attributes
227
What is a row?
an entry of data
228
What is Array.prototype.filter useful for?
anytime you need to filter an array
229
what is array.map useful for?
mutating an entire array based on a transform function
230
What is "syntactic sugar"?
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.
231
What is the typeof an ES6 class?
function
232
Describe ES6 class syntax.
class keyword, name of class, class body, prototype methods
233
what is refactoring?
In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.
234
What must the return value of myFunction be if the following expression is possible? myFunction()();
function
235
``` What does this code do? const wrap = value => () => value; ```
``` recursion wrap() returns the function [()=>value] ```
236
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
when it is defined, lexical scope
237
What allows JavaScript functions to "remember" values from their surroundings?
closures due to lexical scope
238
What does the acronym LIFO mean?
last in first out
239
What methods are available on a Stack data structure?
pop push peak print
240
What must you do to access the value at an arbitrary point in a stack (not just the "top")?
pop off the values in the stack until you reach the one you want