Javascript Flashcards

1
Q

What is the purpose of variables?

A

To give us a location to store data to use in the future

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

How do you declare a variable?

A

By using var, setting a variable name, then using the = operator to set it to a 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

Using the = 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

It is sensitive to capitalizations

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 a numerical or mathematical value

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 store a true or false value

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 used to set a variable to a value (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

State the variable name, assignment operator, set it to new value

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

What is the difference between null and undefined?

A

Null is an assigned value and explicitly means nothing. Undefined means a variable has been declared, but the value has not yet been defined

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

It adds readability and gives more context

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

Give five examples of JavaScript primitives.

A

String, number, boolean, null, undefined

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

What data type is returned by an arithmetic operation?

A

A number data type

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

What is string concatenation?

A

When you combine multiple strings 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 + plus operator serve in JavaScript?

A

To perform addition or concatenate multiple strings together to create one value

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

A shorthand operator that allows you to add the value from the right operand to the left operand, then assigns the new value to the left operand

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

What are objects used for?

A

To store multiple data points or properties within a variable

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

What are object properties?

A

A collection of values attached to 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 variable assigned to multiple properties using curly brackets, then a list of properties and their values separated by a colon, comma to separate multiple properties.

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

Keyword “delete” then the object.property or object[‘property’]

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

getAttribute, setAttribute

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

What are arrays used for?

A

To store a list of elements that we can access by a single variable. To-do lists, lists of users, etc are all great ways to leverage arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
Declare var, variable name, assignment operator, square brackets, and put each value in the brackets, separated by commas
26
How are arrays different from "plain" objects?
Objects use properties and values, while arrays have values only. Objects don’t have an order, while arrays do. Objects use dot notation while arrays use bracket notation.
27
What number represents the first index of an array?
0
28
What is the length property of an array?
It tells you how many items are in your array
29
How do you calculate the last index of an array?
Array.length minus 1
30
What is a function in JavaScript?
A set of statements that performs a task or calculates a value
31
Describe the parts of a function definition.
Function keyword, function name, parenthesis for parameters, curly bracket for function code block, return keyword, then expression, closed curly bracket to close declaration
32
Describe the parts of a function call.
Function name, parenthesis, arguments.
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
Parameters vs arguments. Function definition has function key work and code block. Function call contains the function name and parenthesis to call it.
34
What is the difference between a parameter and an argument?
Parameters are properties of a function definition. Arguments are properties of a function call.
35
Why are function parameters useful?
Parameters allow us to pass information or instructions into functions
36
What two effects does a return statement have on the behavior of a function?
A return statement ends the execution of a function, and returns control to the calling function
37
Why do we log things to the console?
To ensure the code is working properly and to debug code
38
What is a method?
a property of an object that contains a function definition
39
How is a method different from any other function?
A method has to be attached to an object
40
How do you remove the last element from an array?
Pop method
41
How do you round a number down to the nearest integer?
Floor method
42
How do you generate a random number?
math.Random
43
How do you delete an element from an array?
Splice
44
How do you append an element to an array?
Push
45
How do you break a string up into an array?
Split
46
Do string methods change the original string? How would you check if you weren't sure?
They do NOT change the original string. Do it then console log it.
47
Roughly how many string methods are there according to the MDN Web docs?
40 or so
48
Is the return value of a function or method useful in every situation?
no
49
Roughly how many array methods are there according to the MDN Web docs?
30 or so
50
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
51
Give 6 examples of comparison operators.
>, =, <=, >=, ===, !==
52
What data type do comparison expressions evaluate to?
boolean
53
What is the purpose of an if statement?
To guide a program to make decisions based on specified criteria
54
Is else required in order to use an if statement?
no
55
Describe the syntax (structure) of an if statement.
If keyword, parenthesis for condition, curly brace for condition code block, return
56
What are the three logical operators?
&&, ||, and !
57
How do you compare two different expressions in the same condition?
Logical operators
58
What is the purpose of a loop?
Loops allow you to repeat a process over and over without having to write the same (potentially long) instructions each time you want your program to perform a task.
59
What is the purpose of a condition expression in a loop?
an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running.
60
What does "iteration" mean in the context of loops?
Is the single time a loop code block executes
61
When does the condition expression of a while loop get evaluated?
Before each iteration
62
When does the initialization expression of a for loop get evaluated?
Beginning of the loop
63
When does the condition expression of a for loop get evaluated?
Before each iteration takes place
64
When does the final expression of a for loop get evaluated?
After each iteration takes place
65
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
66
What does the ++ increment operator do?
Increases the value of a variable by 1, then substitutes the new value
67
How do you iterate through the keys of an object?
For in loop
68
Why do we log things to the console?
To see what the code is doing / debugging
69
What is a "model"?
Contains all the logic in Javascript
70
Which "document" is being referred to in the phrase Document Object Model?
The HTML document
71
What is the word "object" referring to in the phrase Document Object Model?
A model of the document being represented by javascript objects
72
What is a DOM Tree?
A representative chunk of the page built as javascript objects
73
Give two examples of document methods that retrieve a single element from the DOM.
getElementById() & ***querySelector()***
74
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementsByClassName() & ***querySelectorAll()***
75
Why might you want to assign the return value of a DOM query to a variable?
To use again or reference later
76
What console method allows you to inspect the properties of a DOM element object?
Console.dir (directory method)
77
Why would a < script > tag need to be placed at the bottom of the HTML content instead of at the top?
The HTML document loads from top to bottom. It needs to come after the HTML elements so the DOM content is there before the script loads.
78
What does document.querySelector() take as its argument and what does it return?
It takes a string as an argument (CSS selector) and returns the first instance of that element that matches in the HTML document
79
What does document.querySelectorAll() take as its argument and what does it return?
It takes a string as an argument (CSS selector) and returns a node list. A node list is not a DOM element.
80
Why do we log things to the console?
To see what the code is doing. Great debugging tool.
81
What is the purpose of events and event handling?
To react to something the user does. Event handling is the setup of the response of what happens when the user does something.
82
Are all possible parameters required to use a JavaScript method or function?
No. You can use just the one or ones that are relevant for the specific task
83
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener()
84
What is a callback function?
A function definition being passed as an argument to another function
85
What object is passed into an event listener callback when the event fires?
The event object, or the object with all the information about the event that just occurred
86
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
It stores the DOM element where the event originated from (the start point of the event). Console log it.
87
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
The first is setting up an event listener that calls the function handleClick when a user clicks. The second is executing the function handleClick right this second. There is no return so it will be undefined.
88
What is the className property of element objects?
used to set or return the value of an element's class attribute
89
How do you update the CSS class attribute of an element using JavaScript?
Use className or setAttribute
90
What is the textContent property of element objects?
Represents the text content of the node and it’s descendants
91
How do you update the text within an element using JavaScript?
textContent
92
Is the event parameter of an event listener callback always useful?
No. If you know what you want to do you probably don’t need it.
93
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
94
Why is storing information about a program in variables better than only storing it in the DOM?
Organization, simplicity, and to refer to it later
95
What event is fired when a user places their cursor in a form control?
Focus
96
What event is fired when a user's cursor leaves a form control?
Blur
97
What event is fired as a user changes the value of a form control?
Input
98
What event is fired when a user clicks the "submit" button within a form?
Submit
99
What does the event.preventDefault() method do?
Prevents the default behavior from happening
100
What does submitting a form without event.preventDefault() do?
It would refresh the page because it doesn’t stop the default action
101
What property of a form element object contains all of the form's controls.
HTMLFormElements
102
What property of a form control object gets and sets its value?
Value
103
What is one risk of writing a lot of code without checking to see if it works so far?
If you get an error, you won't know where or why it’s not working
104
What is an advantage of having your console open when writing a JavaScript program?
You can see errors in real time which allows you to know exactly where in the code it’s happening
105
Does the document.createElement() method insert a new element into the page?
No. It creates a new element node, but it’s not attached to the DOM yet
106
How do you add an element as a child to another element?
appendChild method
107
What do you pass as the arguments to the element.setAttribute() method?
Two strings. The first being the name of the attribute, the second is the value of the attribute
108
What steps do you need to take in order to insert a new element into the page?
Query the parent element, create a new element, append new element to parent element
109
What is the textContent property of an element object for?
to set text content for the HTML element or get the text content written inside that element
110
Name two ways to set the class attribute of a DOM element.
setAttribute method or className
111
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
Makes it reusable. Debugging.
112
What is the event.target?
The target event property returns the element that triggered the event
113
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event bubbling
114
What DOM element property tells you what type of element it is?
tagName property
115
What does the element.closest() method take as its argument and what does it return?
It takes a selector as an argument and returns the closest ancestor Element or itself, which matches the selectors
116
How can you remove an element from the DOM?
The remove method on the element we want to remove
117
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?
Dom event delegation. Put eventlistener on the parent.
118
What is the event.target?
The target event property returns the element that triggered the event.
119
What does the element.matches() method take as an argument and what does it return?
It takes a CSS selector as a string and returns a boolean
120
How can you retrieve the value of an element's attribute?
getAttribute method
121
At what steps of the solution would it be helpful to log things to the console?
At every step
122
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 have go create event listeners for each element
123
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’d have to write a bunch of conditionals
124
What is JSON?
JSON stands for JavaScript Object Notation. Converts objects and arrays into a string for data transfer.
125
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.
126
Why are serialization and deserialization useful?
It allows us to transfer data in a uniform way
127
How do you serialize a data structure into a JSON string using JavaScript?
Use the stringify method of the JSON object. Pass in an argument that will be an object or array and the return of the method will be a string.
128
How do you deserialize a JSON string into a data structure using JavaScript?
Use the parse method of the JSON object. Pass in a string as an argument and it will return an object or an array.
129
How do you store data in localStorage?
setItem method of the local storage object. Arguments are a string of the name of the key, and the string of the value.
130
How do you retrieve data from localStorage?
getItem method of the localStorage object. The argument is the key of the value you want to retrieve.
131
What data type can localStorage save in the browser?
Strings
132
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.
133
What is a method?
A method is a function stored in a property of an object
134
How can you tell the difference between a method definition and a method call?
A method definition contains the word ‘function’ to define it with a code block that contains the logic for the method, while the method call is the name of the function followed by ()
135
Describe method definition syntax (structure).
Object name, property, colon, function definition
136
Describe method call syntax (structure).
object.Method name with ()
137
How is a method different from any other function?
It’s stored on properties of objects only
138
What is the defining characteristic of Object-Oriented Programming?
In OOP, objects can contain both data and behavior. Methods can use both within the object.
139
What are the four "principles" of Object-Oriented Programming?
encapsulation, abstraction, inheritance and polymorphism
140
What is "abstraction"?
hide the implementation details and highlight an object's essential features to the users
141
What does API stand for?
Application programming interface
142
What is the purpose of an API?
to give programmers a way to interact with a system in a simplified, consistent fashion.
143
What is this in JavaScript?
‘This’ refers to the object it’s being invoked on
144
What does it mean to say that this is an "implicit parameter"?
The object on which the method is called. It is called implicit because we don't declare it when we declare the method.
145
When is the value of this determined in a function; call time or definition time?
Call time
146
``` 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); } }; ```
‘This’ is nothing because the greet function was never called
147
Given the above character object, what is the result of the following code snippet? Why? character.greet();
“It’s-a-me Mario!” Because the method is being called on the character object.
148
``` Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello(); ```
It will return undefined because hello() is not attached to an object
149
How can you tell what the value of this will be for a particular function or method definition?
You don’t know
150
How can you tell what the value of this is for a particular function or method call?
If the method is attached to an object on the left using dot notation, ‘this’ will return values from the object
151
What kind of inheritance does the JavaScript programming language use?
Prototypal inheritance
152
What is a prototype in JavaScript?
Prototypes are the mechanism by which JavaScript objects inherit features from one another
153
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?
setprototypeof() on the object
154
If an object does not have its own property or method by a given key, where does JavaScript look for it?
It will look for it in the prototype
155
What does the new operator do?
lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.In short, Creates a new blank object, sets the prototype, makes the new object this, then returns the object at the end of the object.
156
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype property which stores an object
157
What does the instanceof operator do?
check the type of an object at the run time
158
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.
159
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()
160
How can you set up a function to be called repeatedly without using a loop?
setInterval()
161
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
There is no time delay
162
What do setTimeout() and setInterval() return?
Returns an ID that is a positive integer value which identifies the timer created by the call
163
What is a client?
A program that is requesting data
164
What is a server?
A program that provides data
165
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
166
What three things are on the start-line of an HTTP request message?
HTTP method (GET, PUT, POST), request target, and HTTP version
167
What three things are on the start-line of an HTTP response message?
Protocol version, status code, and status text
168
What are HTTP headers?
a field of an HTTP request or response that passes additional context and metadata about the request or response
169
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
170
Is a body required for a valid HTTP request or response message?
No
171
What is AJAX?
Allows you to request data from a server and load it without having to refresh the entire page
172
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
173
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
174
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
Load
175
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Because of prototypal inheritance