JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Used to store data

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

How do you declare a variable?

A

var variableName;

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

var variableName = “string”

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

S != s. Uppercase and lowercase letters will be interpreted differently.

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

Consists of letters and other characters. They are frequently used to add new content to a page and they can contain HTML markup

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

Numeric data types handle numbers.

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

Have one of two values: true or false. Booleans are helpful when determining which part of a script should run.

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

What does the = operator mean in JavaScript?

A

= assigs a value 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

assign a new value to the variable

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

Why is it a good habit to include “labels” when you log values to the browser console?

A

To avoid confusion of having multiple console.logs

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

What is string concatenation?

A

combining multiple strings together

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

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

A

concatenation and addition

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

What does the += “plus-equals” operator do?

A

Adds by a specified value and sets the variable to the value of the expression

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

What are objects used for?

A

storing key-value pairs. Used to model real-life objects

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

What are object properties?

A

storage areas connected to an object that can store data

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

Describe object literal notation.

A

{key: value}

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

How do you remove a property from an object?

A

delete operator

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

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

A

dot notation and bracket notation

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

What are arrays used for?

A

Storing data. Particularly useful for lists.

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

Describe array literal notation.

A

[ value, value,… ]

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

How are arrays different from “plain” objects?

A

They are numerically indexed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What number represents the first index of an array?
0
26
What is the length property of an array?
.length
27
How do you calculate the last index of an array?
array.lenght - 1
28
What is a function in JavaScript?
collection instructions that can be repeated whn called
29
Describe the parts of a function definition.
function keyword, name, parameter, , the start of the code block, return statement, end of the code block
30
Describe the parts of a function call.
functionName(arguments)
31
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call takes arguments in parentheses. a function definition has parameters and code block
32
What is the difference between a parameter and an argument?
a parameter is a place holder (variable). arguments are the values passes to a parameter
33
Why are function parameters useful?
allows you to pass information to a function
34
What two effects does a return statement have on the behavior of a function?
returns a value to function and stops execution of code
35
Why do we log things to the console?
for debugging
36
What is a method?
Actions that are performed on objects
37
How is a method different from any other function?
a method exists as a property on an object. a function can be defined anywhere
38
How do you remove the last element from an array?
.pop( ) method
39
How do you round a number to the nearest integer?
.round( ) method
40
How do you generate a random number?
.random( ) method of the Math object
41
How do you delete an element from an array?
.splice( ) method
42
How do you append an element to an array?
.push( ) method
43
How do you break a string up into an array?
.split( ) method
44
Do string methods change the original string? How would you check if you weren't sure?
No. check with console.log.
45
Roughly how many string methods are there according to the MDN Web docs?
40 - 50
46
Is the return value of a function or method useful in every situation?
No. Some functions just modify and change things. Others are used to return something
47
Roughly how many array methods are there according to the MDN Web docs?
A lot
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.
< , > , <=, >=, ==, ===, !=, !==
50
What data type do comparison expressions evaluate to?
Boolean
51
What is the purpose of an if statement?
Evaluates a condition to determine if code will be run
52
Is else required in order to use an if statement?
No
53
Describe the syntax (structure) of an if statement.
if (condition) { code block }
54
What are the three logical operators?
&& || !
55
How do you compare two different expressions in the same condition?
With a logical operator
56
What is the purpose of a loop?
to run a block of code until a certain condition is met
57
What is the purpose of a condition expression in a loop?
determines whether or not the code is run depending on boolean value
58
What does "iteration" mean in the context of loops?
each pass through a loop
59
When does the condition expression of a while loop get evaluated?
at the beginning of each iteration
60
When does the initialization expression of a for loop get evaluated?
before any code has been run
61
When does the condition expression of a for loop get evaluated?
at the beginning of each iteration
62
When does the final expression of a for loop get evaluated?
at the end of each loop
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?
The increment operator (++) increments (adds one to) its operand and returns a value.
65
How do you iterate through the keys of an object?
for in loop
66
Why do we log things to the console?
For debugging and checking functionality
67
What is a "model"?
A representation of an object
68
Which "document" is being referred to in the phrase Document Object Model?
An HTML Page
69
What is the word "object" referring to in the phrase Document Object Model?
Each object represents a different part of the page loaded in the browser window
70
What is a DOM Tree?
Representation of the DOM that is made up of nodes (document node, element nodes, attribute nodes, and text nodes).
71
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID( ) and querySelector( )
72
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll( )
73
Why might you want to assign the return value of a DOM query to a variable?
It is helpful when you want to work with the element more than once
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?
The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
76
What does document.querySelector() take as its argument and what does it return?
A CSS selector is taken as an argument and the first matching element is returned
77
What does document.querySelectorAll() take as its argument and what does it return?
A CSS selector is taken as an argument and all matching elements are returned in a node list
78
Why do we log things to the console?
For debugging and checking functionality
79
What is the purpose of events and event handling?
To allow the script to respond when certain events and interactions occur with the page
80
What do [] square brackets mean in function and method syntax documentation?
It means it is optional
81
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener( )
82
What is a callback function?
A function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
83
What object is passed into an event listener callback when the event fires?
the event object
84
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
The target property of the Event interface is a reference to the object onto which the event was dispatched. Check MDN documentation to get more information about it.
85
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
The function with parentheses would indicate that it should run when the page loads. The function without parentheses will run when the event is fired.
86
What is the className property of element objects?
The className property gets the value of the class attribute of the specified element.
87
How do you update the CSS class attribute of an element using JavaScript?
.className property | Element.className = cName
88
What is the textContent property of element objects?
represents the text content of the node and its descendants.
89
How do you update the text within an element using JavaScript?
.textContent property | Element.textContent = ' '
90
Is the event parameter of an event listener callback always useful?
Generally useful
91
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
92
Why is storing information about a program in variables better than only storing it in the DOM?
You can access it quicker. Do not rely on the DOM.
93
What event is fired when a user places their cursor in a form control?
focus
94
What event is fired when a user's cursor leaves a form control?
blur
95
What event is fired as a user changes the value of a form control?
input
96
What event is fired when a user clicks the "submit" button within a ?
submit
97
What does the event.preventDefault() method do?
preventDefault() method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
98
What does submitting a form without event.preventDefault() do?
the page would reload.
99
What property of a form element object contains all of the form's controls.
form.elements
100
What property of form a control object gets and sets its value?
value
101
What is one risk of writing a lot of code without checking to see if it works so far?
It will be difficult to find where your code stopped working
102
What is an advantage of having your console open when writing a JavaScript program?
gives you the ability to check code
103
What is the event.target?
a reference to the object onto which the event was dispatched
104
What is the affect of setting an element to display: none?
Turns off the display of an element and acts as if it was not there.
105
What does the element.matches() method take as an argument and what does it return?
a selector is used as an arguments. Returns a boolean
106
How can you retrieve the value of an element's attribute?
getAttribute( )
107
At what steps of the solution would it be helpful to log things to the console?
Everywhere
108
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?
You would need an event listener for each tab and view
109
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?
You would need conditionals for each
110
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
111
What is the event.target?
a reference to the object onto which the event was dispatched
112
What DOM element property tells you what type of element it is?
tagName
113
What does the element.closest() method take as its argument and what does it return?
A selector is used as an argument. It returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. If there isn't such an ancestor, it returns null.
114
How can you remove an element from the DOM?
remove( )
115
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?
using the parent node as the eventlistener
116
What is JSON?
JSON is a text-based data format following JavaScript object syntax. JSON exists as a string — useful when you want to transmit data across a network.
117
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network. Deserialization is the reverse process: turning a stream of bytes into an object in memory.
118
Why are serialization and deserialization useful?
By allowing an Object to be converted into stream of bytes so that it can be transferred over a network or stored in a persistent storage. Vis versa, the string can be pulled from a network and converted to an object.
119
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify
120
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse
121
How to you store data in localStorage?
.setItem( )
122
How to you retrieve data from localStorage?
.getItem( )
123
When does the 'beforeunload' event fire on the window object?
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.
124
What is a method?
A method is a function which is a property of an object. There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or Static Methods which are tasks that are called directly on an object constructor.
125
How can you tell the difference between a method definition and a method call?
Method call- object.method() ``` method definition- method: function() { // } ```
126
Describe method definition syntax (structure).
``` method: function() { // } ```
127
Describe method call syntax (structure).
object.method()
128
How is a method different from any other function?
A method is associated with an object while a function is not
129
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data (as properties) and behavior (as methods).
130
What are the four "principles" of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
131
What is "abstraction"?
Being able to work with (possibly) complex things in simple ways.
132
What does API stand for?
Application programming interface
133
What is the purpose of an API?
the purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
134
What data type can local storage save in the browser
String
135
What is this in JavaScript?
a keyword which contains a value which is defined at call time
136
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.
137
When is the value of this determined in a function; call time or definition time?
call time
138
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); } }; ```
Mario
139
``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ``` Given the above character object, what is the result of the following code snippet? Why? character.greet();
"It's-a-me Mario!" The greet method of the character object is call. This method includes a string that is concatenated with this.FirstName.
140
``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ``` Given the above character object, what is the result of the following code snippet? Why? ``` var hello = character.greet; hello(); ```
undefined. this is attached to hello when it is called with hello. hello does not have a firstName property. this is attached to whoever is calling the function.
141
How can you tell what the value of this will be for a particular function or method definition?
it will refer to the object it is in
142
How can you tell what the value of this is for a particular function or method call?
the value of this can be recognized as "the object to the left of the dot" when the function is called (as a method).
143
What kind of inheritance does the JavaScript programming language use?
prototype based
144
What is a prototype in JavaScript?
An object that contains properties and (predominantly) methods that can be used by other objects.
145
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?
The methods are defined on a "prototype" object and arrays simply borrow those methods when they're needed.
146
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
prototype chain
147
What does the new operator do?
1. Creates a blank, plain JavaScript object; 2. Links (sets the constructor of) the newly created object to another object by setting the other object as its parent prototype; 3. Passes the newly created object from Step 1 as the this context; 4. Returns this if the function doesn't return an object.
148
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
149
What does the instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
150
What is a "callback" function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
151
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( )
152
How can you set up a function to be called repeatedly without using a loop?
setInterval( )
153
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
154
What do setTimeout() and setInterval() return?
The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval()
155
What is Array.prototype.filter useful for?
Creating a new array from an existing array that contains the values that pass a given callback function
156
What is Array.prototype.map useful for?
creating a new array containing the values resulting from calling a provided callback function on each value of an array
157
What is Array.prototype.reduce useful for?
for reducing an array to a single value using a provided callback function
158
What does fetch() return?
a promise
159
What is the default request method used by fetch()?
GET
160
How do you specify the request method (GET, POST, etc.) when calling fetch?
specify the request method as the value of the method property in the init object
161
What must the return value of myFunction be if the following expression is possible? myFunction()();
a function
162
``` What does this code do? const wrap = value => () => value; ```
a function that takes a single parameter and returns a function that returns the value of that parameter
163
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
when it is defined
164
What allows JavaScript functions to "remember" values from their surroundings?
closures