JavaScript Flashcards

1
Q

What is the purpose of variables?

A

to store data values that can be used 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 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

variableName = value;

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

What characters are allowed in variable names?

A

letters, underscore, dollar sign, numbers (but variable name cannot start with a number)

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

variable names with different casing constitute different variables

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 words, letters, and characters

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

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 the value of true/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

assigns 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

= assignment operator

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 must be intentionally assigned to a variable, while undefined is automatically assigned when a variable was not assigned a value

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

to make clear which variables are being logged and in what order

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

number

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

What is string concatenation?

A

combines 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 for numbers and concatenation for strings

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

What data type is returned by comparing two values ( < , > , === , etc)?

A

boolean

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

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

A

adds the value of the operand to the variable 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 related variables and functions

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

What are object properties?

A

variables that are part of an object which have a unique key name and correlated value

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

Describe object literal notation.

A

object in curly braces { } each key separated from its value using a colon, each key/value pair separated by commas

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 objectName.propertyName;

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

objectName.propertyName (dot notation) or objectName[“propertName”] (bracket notation)

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

What are arrays used for?

A

storing a list of values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
square brackets [ ] each value is separated by a comma
26
How are arrays different from "plain" objects?
arrays are ordered and the keys are the index numbers (rather than the property names)
27
What number represents the first index of an array?
0
28
What is the length property of an array?
number of items in the array
29
How do you calculate the last index of an array?
arrayName.length - 1
30
What is a function in JavaScript?
a block of code designed to perform a particular task
31
Describe the parts of a function definition.
function keyword, (optional) function name, comma-separated list of 0+ parameters surrounded by parentheses, curly braces, (optional) return statement
32
Describe the parts of a function call.
function name + parentheses with arguments (if function definition has them)
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
the definition has the function keyword and the code block with curly braces
34
What is the difference between a parameter and an argument?
parameters are placeholders in the function definition; arguments are passed to the function when it is called
35
Why are function parameters useful?
way to provide data to get different results from functions
36
What two effects does a return statement have on the behavior of a function?
1) causes the function to produce a value; 2) exits the function
37
Why do we log things to the console?
to debug
38
What is a method?
a function that is a property of an object
39
How is a method different from any other function?
it is called on an object
40
How do you remove the last element from an array?
.pop( )
41
How do you round a number down to the nearest integer?
Math.floor( )
42
How do you generate a random number?
Math.random( )
43
How do you delete an element from an array?
.splice( ) or .pop( ) or .shift( )
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?
no; can check by logging the string to the console or read up MDN
47
Is the return value of a function or method useful in every situation?
not necessarily
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?
conditionally runs a block of code
52
Is else required in order to use an if statement?
no
53
Describe the syntax (structure) of an if statement.
if keyword, condition in parentheses, curly braces
54
What are the three logical operators?
&& , || , !
55
How do you compare two different expressions in the same condition?
&& or ||
56
What is the purpose of a loop?
to repeat a set of steps
57
What is the purpose of a condition expression in a loop?
to determine when the loop should stop
58
What does "iteration" mean in the context of loops?
the code block being executed once
59
When does the condition expression of a while loop get evaluated?
each time before the code block is executed
60
When does the initialization expression of a for loop get evaluated?
once at the beginning of the loop, before the condition
61
When does the condition expression of a for loop get evaluated?
each time before the code block is executed
62
When does the final expression of a for loop get evaluated?
each time after the code block is executed
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?
adds 1
65
How do you iterate through the keys of an object?
for ( var key in object )
66
What is a "model"?
a representation of something
67
Which "document" is being referred to in the phrase Document Object Model?
the HTML document
68
What is the word "object" referring to in the phrase Document Object Model?
JavaScript objects which represent the different parts of the web page
69
What is a DOM Tree?
an object model that represents the structure of a webpage
70
Give two examples of document methods that retrieve a single element from the DOM.
querySelector( ) , getElementById( )
71
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll( )
72
Why might you want to assign the return value of a DOM query to a variable?
saves the browser from having to look through the DOM tree to find the same elements again
73
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
74
Why would a < script > 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
75
What does document.querySelector( ) take as its argument and what does it return?
takes a CSS selector, returns node of the first matching element
76
What does document.querySelectorAll( ) take as its argument and what does it return?
takes a CSS selector, returns a nodelist of all matching elements
77
What is the purpose of events and event handling?
to enable the web page to interact with users
78
What do [ ] square brackets mean in function and method syntax documentation?
optional
79
What is a callback function?
a function passed into another function as an argument
80
What object is passed into an event listener callback when the event fires?
the event object that contains all relevant info about the event
81
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
the element where the event originated from; can read about it on MDN
82
What is the className property of element objects?
enables you to get and set the value of the class attribute of the specified element
83
What is the textContent property of element objects?
the text that is in the containing element (and its children)
84
How do you update the text within an element using JavaScript?
.textContent
85
Is the event parameter of an event listener callback always useful?
not necessarily because you don't always need info about the event
86
Why is storing information about a program in variables better than only storing it in the DOM?
better way to save and store data values, such as info collected from users
87
What does the transform property do?
allows you to modify the coordinate plane of the element
88
Give four examples of CSS transform functions.
rotate, scale, skew, translate
89
What event is fired when a user places their cursor in a form control?
focus
90
What event is fired when a user's cursor leaves a form control?
blur
91
What event is fired as a user changes the value of a form control?
input
92
What event is fired when a user clicks the "submit" button within a < form >?
submit
93
What does the event.preventDefault() method do?
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)
94
What does submitting a form without event.preventDefault() do?
the browser will automatically reload the page with the form's values in the URL
95
What property of a form element object contains all of the form's controls.
elements
96
What property of a form control object gets and sets its value?
value
97
What is one risk of writing a lot of code without checking to see if it works so far?
makes it difficult to debug, and you might have to rewrite all of it if there are errors
98
What is an advantage of having your console open when writing a JavaScript program?
check and debug code in real time
99
Does the document.createElement() method insert a new element into the page?
no, it creates an element node but it is not on the page yet
100
How do you add an element as a child to another element?
.appendChild( ) or .append( )
101
What do you pass as the arguments to the element.setAttribute() method?
(attributeName, attributeValue)
102
What steps do you need to take in order to insert a new element into the page?
createElement( ) , [ assign .textContent ] , appendChild( )
103
What is the textContent property of an element object for?
to get/set the text content of an element and its children
104
Name two ways to set the class attribute of a DOM element.
element.setAttribute(class, classValue) , or assign value as the className property
105
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
so you can easily repeat the code block without having to redo the work; can make the website dynamic
106
What is the event.target?
the element where the event originated from; a reference to the object onto which the event was dispatched
107
What is the effect of setting an element to display: none?
hides the element and removes it from the document flow
108
What does the element.matches() method take as an argument and what does it return?
takes a CSS selector string, returns a boolean of whether the element is the selector
109
How can you retrieve the value of an element's attribute?
Element.getAttribute( )
110
At what steps of the solution would it be helpful to log things to the console?
after the value of a variable changes
111
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 to add an event listener for each tab
112
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 have to manually set the class name for each tab and view
113
What is a method?
a function that is a property of an object
114
How can you tell the difference between a method definition and a method call?
the method definition has a corresponding object property key, the function keyword, and curly braces which contain the function code block
115
Describe method definition syntax (structure).
an object is defined with a property key and the corresponding function, which has the function keyword, parameters separated by commas in parentheses, and curly braces which contain the function code block
116
Describe method call syntax (structure).
objectName.methodName(arg1, arg2,...)
117
How is a method different from any other function?
it must be called on an object, can access data stored on the object
118
What is the defining characteristic of Object-Oriented Programming?
pairs data with behavior
119
What does API stand for?
Application Programming Interface
120
What is "abstraction"?
simplifying a complex process so that it is easy to use
121
What is the purpose of an API?
simplifies programming through abstraction
122
What is this in JavaScript?
it is an implicit parameter
123
What does it mean to say that this is an "implicit parameter"?
the variable value is available in a function's code block even though it was never included in the function's parameter list or declared with var
124
When is the value of this determined in a function; call time or definition time?
call time
125
``` 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); } }; ```
the object named character
126
Given the above character object, what is the result of the following code snippet? Why? character.greet( );
"It's-a-me, Mario!" - calling the greet method of the character property, which contains the firstName property of this object (the character object)
127
``` Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello( ); ```
"It's-a-me, undefined!" - we do not know what the value of this will be since there is no object to the left of the dot
128
How can you tell what the value of this will be for a particular function or method definition?
the object in which the method is defined
129
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot; if there is no value to the left of the dot when the function is called, this will be the global window object (by default)
130
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
131
What is a prototype in JavaScript?
an object that contains properties and (predominantly) methods that can be used by other objects
132
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?
methods are defined on a "prototype" object and are borrowed when they're needed
133
If an object does not have its own property or method by a given key, where does JavaScript look for it?
the prototype object
134
What does the new operator do?
1) creates a blank object 2) adds a __proto__ property to the new object that links to the constructor function's prototype object 3) binds the newly created object instance as the this context 4) returns this if the function doesn't return an object
135
What property of JavaScript functions can store shared behavior for instances created with new?
__proto__
136
What does the instanceof operator do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object (returns a boolean)
137
What is a "callback" function?
a function passed into another function as an argument
138
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( )
139
How can you set up a function to be called repeatedly without using a loop?
setInterval( )
140
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
141
What do setTimeout() and setInterval() return?
a positive integer value which identifies the timer created (ID)
142
What is AJAX?
a technique for loading data into part of a page without having to refresh the entire page
143
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
144
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
145
Bonus Question: An XMLHttpRequest object has an addEventListener( ) method just like DOM elements. How is it possible that they both share this functionality?
both have the EventTarget prototype within their prototype chains
146
What is the purpose of a conditional?
to make decisions in code
147
What is a code block? What are some examples of a code block?
the code within curly braces, such as the code block of functions, conditionals, and loops
148
What does block scope mean?
the value of the variable is available only within the code block
149
What is the scope of a variable declared with const or let?
within the code block
150
What is the difference between let and const?
variables declared with let can be reassigned while variables declared with const cannot
151
Why is it possible to .push( ) a new value into a const variable that points to an Array?
because we are not reassigning a new value, just modifying the contents of the original value
152
What is the syntax for writing a template literal?
text is wrapped in backticks ( ` )
153
What is "string interpolation"?
substituting part of a string for the values of variables or expressions
154
What is destructuring, conceptually?
a way to assign the parts of an object/array to individual variables
155
What is the syntax for Object destructuring?
const/let { propertyName1: alias1, propertyName2: alias2 } = objectName;
156
What is the syntax for Array destructuring?
const/let [ var1, var2, var3 ] = arrayName;
157
How can you tell the difference between destructuring and creating Object/Array literals?
the object/array literal is on the left-hand side of the = for destructuring, and it is on the right-hand side for creating
158
What is the syntax for defining an arrow function?
parentheses around the parameters, followed by => , followed by curly braces containing the code block
159
When an arrow function's body is left without curly braces, what changes in its functionality?
must be an expression - do not need a return statement
160
How is the value of this determined within an arrow function?
the arrow function captures the this value of the enclosing context instead of creating its own this context