JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store information, so it can be retrieved 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 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

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

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (0–9).

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

Their exact case must match.

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 hold 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 hold information that relates to arithmetic calculations.

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 check whether a value is true or false and make decisions based on that.

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

Assigning a value.

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

name = newValue;

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 has been set by the user, while undefined is on the computers end.

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

So that you should be able to see clearly what relates to what, and also for the benefit of others.

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

Give five examples of JavaScript primitives.

A

number / string / boolean / undefined / null

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

Adding a string to another string using +

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

Concatenation of strings / addition

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

Add new value, and reassigns 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 store properties and methods together.

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

What are object properties?

A

Like variables, they contain information about specifics of an object.

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

Describe object literal notation.

A

{ }

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

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

To store lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
[ ]
26
How are arrays different from "plain" objects?
They have a specific order.
27
What number represents the first index of an array?
0
28
What is the length property of an array?
How many items the array contains.
29
How do you calculate the last index of an array?
array.length - 1
30
Why should arrays have similar data types and structure?
Will be much easier to work with, and loop over.
31
What is a function in JavaScript?
A recipe, a set of steps.
32
Describe the parts of a function definition.
``` function functionName(params) { code optional return } ```
33
Describe the parts of a function call.
functionName(params);
34
When comparing them side-by-side, what are the differences between a function call and a function definition?
``` function keyword. code block { } ```
35
What is the difference between a parameter and an argument?
parameter is a placeholder, no value yet. Argument is when we are giving it a value.
36
Why are function parameters useful?
To supply additional information, to make it reusable
37
What two effects does a return statement have on the behavior of a function?
Code stops running there. | Returns a value that can be used.
38
Why do we log things to the console?
To see if they behave as expected.
39
What is a method?
A function of an object.
40
How is a method different from any other function?
Since it belongs to an object, it needs to be called with the object.
41
How do you remove the last element from an array?
array.pop( )
42
How do you round a number down to the nearest integer?
Math.floor( )
43
How do you generate a random number?
Math.random ( )
44
How do you delete an element from an array?
array.splice(1, 1, add)
45
How do you append an element to an array?
array.push( )
46
How do you break a string up into an array?
string.split(' ')
47
Do string methods change the original string? How would you check if you weren't sure?
No
48
Is the return value of a function or method useful in every situation?
No
49
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
50
Give 6 examples of comparison operators.
=== / !== / < / > / <= / >=
51
What data type do comparison expressions evaluate to?
Boolean
52
What is the purpose of an if statement?
To check whether something is true, and do something based on that
53
Is else required in order to use an if statement?
No
54
Describe the syntax (structure) of an if statement.
if (expression) { code }
55
What are the three logical operators?
&& / || / !
56
How do you compare two different expressions in the same condition?
With && ||
57
What is the purpose of a loop?
To do steps repeatedly until a certain condition is met.
58
What is the purpose of a condition expression in a loop?
To see whether the loop should continue.
59
What does "iteration" mean in the context of loops?
Each round
60
When does the condition expression of a while loop get evaluated?
Before the code block begins
61
When does the initialization expression of a for loop get evaluated?
First thing
62
When does the condition expression of a for loop get evaluated?
Before each iteration of the loop
63
When does the final expression of a for loop get evaluated?
After each iteration of the loop
64
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
65
What does the ++ increment operator do?
Adds 1
66
How do you iterate through the keys of an object?
for (var key in object)
67
What is a "model"?
Recreation of something, in this case, the HTML page
68
Which "document" is being referred to in the phrase Document Object Model?
The HTML document
69
What is the word "object" referring to in the phrase Document Object Model?
Javascript objects representing elements on the page
70
What is a DOM Tree?
The presentation of the nodes in a tree-like structure
71
Give two examples of document methods that retrieve a single element from the DOM.
getElementById / 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?
To be able to use it again in the future
74
What console method allows you to inspect the properties of a DOM element object?
dir
75
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
So that it can first read all elements and then take any action required
76
What does document.querySelector() take as its argument and what does it return?
css selector, returns the first match
77
What does document.querySelectorAll() take as its argument and what does it return?
css selector, returns all matches as a node list
78
What is the purpose of events and event handling?
So that when events happen, we can have something which handles it
79
What do [] square brackets mean in function and method syntax documentation?
Optional
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 which is called by another function
82
What object is passed into an event listener callback when the event fires?
event
83
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
The element that fired the event. MDN
84
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
1) We are telling the browser to call the function | 2) We are calling the function and getting its return which is undefined
85
What is the className property of element objects?
Can get or set the classname on an element
86
How do you update the CSS class attribute of an element using JavaScript?
var.className
87
What is the textContent property of element objects?
Can get or set the text on an element
88
How do you update the text within an element using JavaScript?
var.textContent
89
Is the event parameter of an event listener callback always useful?
No
90
Why is storing information about a program in variables better than only storing it in the DOM?
Easier to retrieve
91
What event is fired when a user places their cursor in a form control?
focus
92
What event is fired when a user's cursor leaves a form control?
blur
93
What event is fired as a user changes the value of a form control?
input
94
What event is fired when a user clicks the "submit" button within a form?
submit
95
What does the event.preventDefault() method do?
That the page shouldn't refresh without hitting the submit button
96
What does submitting a form without event.preventDefault() do?
The page would refresh.
97
What property of a form element object contains all of the form's controls.
.elements
98
What property of form control object gets and sets its value?
.name.value
99
Does the document.createElement() method insert a new element into the page?
No
100
How do you add an element as a child to another element?
appendChild( )
101
What do you pass as the arguments to the element.setAttribute() method?
name, value
102
What steps do you need to take in order to insert a new element into the page?
createElement, Add to the Dom
103
What is the textContent property of an element object for?
To add or get the text content
104
Name two ways to set the class attribute of a DOM element.
element.className / element.setAttribute( )
105
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Reusable / Easier debugging / returns value
106
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event bubbling
107
What DOM element property tells you what type of element it is?
event.target.tagName
108
What does the element.closest() method take as its argument and what does it return?
css selector, it returns the closest parent
109
How can you remove an element from the DOM?
element.remove( )
110
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?
event delegation on the parent
111
What is the affect of setting an element to display: none?
Hides it
112
What does the element.matches() method take as an argument and what does it return?
css selector / boolean
113
How can you retrieve the value of an element's attribute?
getAttribute( )
114
At what steps of the solution would it be helpful to log things to the console?
All steps
115
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?
Requiring to add an extra event handler for each
116
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?
Each step would have to be manually repeated
117
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
118
What are serialization and deserialization?
Making it into a string / Making the string into a object
119
Why are serialization and deserialization useful?
To send over the network / To be able to use in more dynamic ways (objects, arrays)
120
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( )
121
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( )
122
How do you store data in localStorage?
localStorage.setItem( )
123
How to you retrieve data from localStorage?
localStorage.getItem( )
124
What data type can localStorage save in the browser?
Only string
125
When does the 'beforeunload' event fire on the window object?
Before you leave the page or refresh
126
How can you tell the difference between a method definition and a method call?
method definition = function keyword / code block | method call = obj.methodName( )
127
Describe method definition syntax (structure).
propertyName: function( ) { code }
128
Describe method call syntax (structure).
obj.methodName( )
129
How is a method different from any other function?
It belongs to an object
130
What is the defining characteristic of Object-Oriented Programming?
Data together with methods form objects
131
What are the four "principles" of Object-Oriented Programming?
Abstraction / Encapsulation / Inheritance / Polymorphism
132
What is "abstraction"?
Hiding the implementation of a complex process
133
What does API stand for?
Application Programming Interface
134
What is the purpose of an API?
To be able to access a complex process without needing to know how it works
135
What is this in JavaScript?
An implicit parameter.
136
What does it mean to say that this is an "implicit parameter"?
It is available even though it has not been specifically defined
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); } }; ``` Given the above character object, what is the result of the following code snippet? Why? character.greet(); ``` Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello(); ```
The character object It's me Mario, because the character object is calling it. It's me undefined, because the default window object is calling it.
139
How can you tell what the value of this will be for a particular function or method definition?
You can't
140
How can you tell what the value of this is for a particular function or method call?
The object to the left of the .
141
What kind of inheritance does the JavaScript programming language use?
Prototype
142
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?
Through getting it from the prototype
143
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
In the prototype
144
What does the new operator do?
Creates a blank, plain JavaScript object; Links the newly created object to another object by setting the other object as its parent prototype; Passes the newly created object as the this context; Returns this if the function doesn't return an object.
145
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype
146
What does the instanceof operator do?
Checks whether that object is an instance of another object
147
What is a "callback" function?
A function within a function
148
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( )
149
How can you set up a function to be called repeatedly without using a loop?
setInterval ( )
150
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
151
What do setTimeout() and setInterval() return?
The id associated with them
152
What is a client?
A software or computer requesting information
153
What is a server?
Something that is able to provide a service
154
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
155
What three things are on the start-line of an HTTP request message?
http method / Url / Protocol
156
What three things are on the start-line of an HTTP response message?
Protocol / Status code/ Status text
157
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
158
Is a body required for a valid HTTP request or response message?
No
159
What is AJAX?
A programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest. Ajax allows you to update parts of the DOM of an HTML page instead without the need for a full page refresh. Ajax also lets you work asynchronously, meaning your code continues to run while the targeted part of your web page is trying to reload (compared to synchronously, which blocks your code from running until that part of your page is done reloading).
160
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
161
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
162
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
163
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Share the same prototype
164
What are HTTP headers?
Additional information relating to a request or response