JavaScript Flashcards

(174 cards)

1
Q

What is the purpose of variables?

A

data storage

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

variable name = variable 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, numbers, $, 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

two variables can share the same name but have different capitalization, making them different variables (don’t do this; it’s bad practice)

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

storage of text data

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

storage of numeric data types

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

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

variable name = 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: intentional absence created by a human

- undefined: lack of value recognized by JavaScript

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

for clarity and to describe 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, 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

numeric

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

What is string concatenation?

A

adding two or more strings together to create a new string

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

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

adds another value to variable and the result of that expression gets assigned 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 group together a set of 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

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

Describe object literal notation

A

var object = {

};

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

by using the 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

dot notation or 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

store lists of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation
var name = [ ];
26
How are arrays different from "plain" objects?
only uses numeric index, contains a length property that is constantly updated, multiple methods to interact with arrays
27
What number represents the first index of an array?
0 (zero)
28
What is the length property of an array?
stores (true) count of number of items are in array
29
How do you calculate the last index of an array?
subtract 1 from the variable
30
What is a function in JavaScript?
a set of code block for performing a task that is reusable
31
Describe the parts of a function definition
function keyword, name, parameters list, {code block};
32
Describe the parts of a function call
function name(arguments); | there can be 0 to many arguments
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
``` function call: there is a function name followed by (); - executes code ``` function definition: there is a function keyword and a code block - code does not run - gives function a name
34
What is the difference between a parameter and an argument?
parameter: placeholder for argument value - value is unknown until function gets called and argument(s) is passed argument: a known value that gets passed when a function is called
35
Why are function parameters useful?
- serves as a placeholder for arguments - allows for varying results based on arguments that will get passed later on - reusable behavior
36
What two effects does a return statement have on the behavior of a function?
- causes function to produce a value that can be used in programming - prevents any more code in function code block from being run
37
Why do we log things to the console?
debugging
38
What is a method?
- a function which is a property of an object - in JavaScript functions themselves are objects, so, in that context, a method is actually an object reference to a function
39
How is a method different from any other function?
methods are part of 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()
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?
strings are immutable; call a method to check the values
47
Roughly how many string methods are there according to the MDN Web Docs?
50+
48
Is the return value of a function or method useful in every situation?
no, it varies by situation
49
Roughly how many array methods are there according to the MDN Web docs?
50+
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
``` == equal to === strict equal to != not equal to > >= ```
52
What data type do comparison expressions evaluate to?
boolean
53
What is the purpose of an if statement?
allows for decision-making and different pathways for code
54
Is else required in order to use an if statement?
no
55
Describe the syntax (structure) of an if statement
if (condition) { statement };
56
What are the three logical operators?
&& logical and || logical or ! logical not
57
How do you compare two different expressions in the same condition?
(expression) && (expression) | expression) || (expression
58
What is the purpose of a loop?
to check a condition repeatedly until it returns false
59
What is the purpose of a condition expression in a loop?
tells the loop when to stop
60
What does "iteration" mean in the context of loops?
how ever many times the code block runs
61
When does the condition expression of a while loop get evaluated?
before each iteration
62
When does the initialization expression for a for loop get evaluated?
once, before the loop begins
63
When does the condition expression for a for loop get evaluated?
after initialization and before each loop iteration
64
When does the final expression of a for loop get evaluated?
after code block runs and before the condition runs again
65
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break keyword
66
What does the ++ increment operator do?
adds one, returning a value that substitutes the current value
67
How do you iterate through the keys of an object?
using for...in loop
68
Why do we log things to the console?
to look at the outputs we're working with and to also debug when a problem occurs
69
What is a "model?"
a replica of something
70
Which "document" is being referred to in the phrase Document Object Model?
HTML document
71
What is the word "object" referring to in the phrase Document Object Model?
data type object
72
What is a DOM tree?
a representation of the objects in HTML, including the parent element, child element, nodes
73
Give two examples of document methods that retrieve a single element from the DOM
.querySelector('css selector') .getElementById('id')
74
Give one example of a document method that retrieves multiple elements from the DOM at once
.querySelectorAll('css selector')
75
Why might you want to assign the return value of a DOM query to a variable?
to access it again later (also for efficiency)
76
What console method allows you to inspect the properties of a DOM element object?
console.dir | dir = directory
77
Why would a < script > tag need to be placed at the bottom of the HTML content instead of at the top?
HTML document loads from top to bottom so the browser needs to parse all elements before JavaScript code can access it
78
What does document.querySelector() take as its argument and what does it return?
CSS selector; the first matching element
79
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector; all matching elements (nodelist)
80
What is the purpose of events and event handling?
creates interactivity with the user; code responds to events triggered by the user
81
Are all possible parameters required to use a JavaScript method or function?
no because there could be a function with no parameters
82
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener
83
What is a callback function?
a function passed into another function as an argument, which is then invoked inside outer function to complete an action
84
What object is passed into an event listener callback when the event fires?
an object with all data about the event that just occurred
85
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
property of the event object and the element where the event occurred; check MDN
86
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
- the first snippet is a function definition - the second snippet is a function call - the event handler will be undefined because the function call gets replaced with a return - there is never a return for an event handler function
87
What is the className property of element objects?
property that sets the value of the class attribute of the specified element
88
How do you update the CSS class attribute of an element using JavaScript?
query for the element, get new value, re-assign value to element using .className
89
What is the textContent property of element objects?
text content of specified node and its descendants
90
How do you update the text within an element using JavaScript?
query for the element, get new value, re-assign value to element using .textContent
91
Is the event parameter of an event listener callback always useful?
no; only needed when you need to know where the event occurred
92
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 because you would need to retrieve text, convert it to a number, then return it - variables are easier to work with and easy to identify
93
Why is storing information about a program in variables better than only storing it in the DOM?
- more efficient and is easily accessible by JavaScript | - data should be stored in JavaScript to keep track of values
94
What event is fired when a user places their cursor in a form control?
focus
95
What event is fired when a user's cursor leaves a form control?
blur
96
What event is fired as a user changes the value of a form control?
input
97
What event is fired when a user clicks the "submit" button within a ?
submit
98
What does the event.preventDefault() method do?
prevents default behavior from happening | e.g., browser automatically reloading page with form values in URL
99
What does submitting a form without event.preventDefault() do?
it reloads the page with form values
100
What property of a form element object contains all of the form's controls?
.elements | elements property
101
What property of a form control object gets and sets its value?
.value | value property
102
What is one risk of writing a lot of code without checking to see if it works so far?
not knowing where a problem occurred and its effect on other lines of code
103
What is an advantage of having your console open when writing a JavaScript program?
see code in action, be able to spot problems and fix them as needed
104
Does the document.createElement() method insert a new element into the page?
it creates an element node, but it's on the page or visible yet
105
How do you add an element as a child to another element?
.appendChild | append = to add to the end
106
What do you pass as the arguments to the element.setAttribute() method?
(name, value)
107
What steps do you need to take in order to insert a new element into the page?
1. create element 2. give it content 3. query DOM for target parent 4. add to DOM by appending child to parent
108
What is the textContent property of an element object for?
- text content of the node and its descendants | - it can be used to retrieve and set text content of an element
109
Name two ways to set the class attribute of a DOM element.
.setAttribute .querySelector for the element then assign string to class name property
110
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
- can test the function to see what it returns when an argument is passed - can reuse the code later
111
What is the event.target?
it returns the element that was triggered by an event
112
Why is it possible to listen for events on one element that actually happen on its descendent elements?
event bubbling
113
What DOM element property tells you what type of element it is?
.tagName
114
What does the element.closest() method take as its argument and what does it return?
string selector; closest ancestor element
115
How can you remove an element from the DOM?
.remove()
116
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?
wrap it on a parent element then add event listener on that parent element
117
What is the affect of setting an element to display: none?
it will disappear and be removed from document flow
118
What does the element.matches() method take as an argument and what does it return?
selector string; boolean
119
How can you retrieve the value of an element's attribute?
.getAttribute()
120
At what steps of the solution would it be helpful to log things to the console?
all steps
121
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?
- create custom individual event handler for each tab | - use .querySelector() instead of .querySelectorAll()
122
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?
add a new if statement for each tab
123
What is JSON?
- JavaScript Object Notation | - text-based data format that exists as a string, following JavaScript object syntax
124
What are serialization and deserialization?
- serialization: converting object to a string/series of bytes - deserialization: converting string/stream of bytes to a native object
125
Why are serialization and deserialization useful?
useful for when you want to transmit data across a network or need data to be stored on a disk
126
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
127
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
128
How to you store data in localStorage?
.setItem(key, value)
129
How to you retrieve data from localStorage?
storage.getItem(keyName) | if empty, it will return null
130
What data type can localStorage save in the browser?
strings
131
When does the 'beforeunload' event fire on the window object?
when the window is about to close or refresh
132
What is a method?
a function which is a property of an object
133
How can you tell the difference between a method definition and a method call?
- method definition: contains a function keyword, code block, and is being assigned to a property - method call: only contains name of object followed by the method ()
134
Describe method definition syntax (structure).
``` property name : function (parameters) { //code block; }; ```
135
Describe method call syntax (structure).
object.method();
136
How is a method different from any other function?
it's a property of an object
137
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods)
138
What are the four "principles" of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
139
What is "abstraction"?
simplifying complex concepts
140
What does API stand for?
Application Programming Interface
141
What is the purpose of an API?
software abstraction; simplifying complex behavior so people can use it without having to fully understand how something actually works
142
What is this in JavaScript?
an implicit parameter of all JavaScript functions
143
What does it mean to say that this is an "implicit parameter"?
always present in function's code block even though it was never included in the parameter list or declared with a variable
144
When is the value of this determined in a function; call time or definition time?
call time
145
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; there is no "this" yet | - method must be called first
146
Given the character object below, what is the result of the following code snippet? character.greet(); Why? ``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ```
It's-a-me, Mario! - the greet method is being called on the character object
147
Given the character object below, what is the result of the following code snippet? ``` var hello = character.greet; hello(); ``` Why? ``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ```
It's-a-me, undefined! - "this" is the window which doesn't have a firstName property
148
How can you tell what the value of this will be for a particular function or method definition?
you can't bc it doesn't have a value until it is called
149
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 when the function is called
150
What kind of inheritance does the JavaScript programming language use?
prototypal inheritance
151
What is a prototype in JavaScript?
a model of an object that other objects can use to build upon
152
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?
by using the prototype object, which contains properties and methods that can be used by other objects
153
If an object does not have its own property or method by a given key, where does JavaScript look for it?
it borrows from the prototype object
154
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
155
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
156
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 (return value is a boolean)
157
What is a "callback" function?
a function that gets passed into another function as an argument
158
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() function
159
How can you set up a function to be called repeatedly without using a loop?
setInterval() function
160
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
zero
161
What do setTimeout() and setInterval() return?
a numeric, non-zero value (ID) which identifies the timer created
162
What is a client?
a piece of software that accesses a service made available by a server as part of the client-server model
163
What is a server?
a computer hardware or software that provides functionality for other programs or devices known as clients (wait for a request from client and provide what was requested)
164
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
165
What three things are on the start-line of an HTTP request message?
1. an HTTP method that describes the action to be performed 2. the request target, usually a URL or absolute path of the protocol, port, and domain 3. the HTTP version, which defines the structure of the remaining message, acting as an indicator of the expected version to use for the response
166
What three things are on the start-line of an HTTP response message?
1. the protocol version, usually HTTP/1.1 2. a status code (success or failure) of the request 3. a status text providing description of the status code to help person understand the HTTP message
167
What are HTTP headers?
- an HTTP header consists of its case-insensitive name followed by a colon (:), then by its value HTTP headers... - let the client and the server pass additional information with an HTTP request or response - allow you to provide additional metadata about the request or response
168
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
169
Is a body required for a valid HTTP request or response message?
- a body is optional | - applicable in certain cases like 201, which indicates that data was successfully created
170
What is AJAX?
a technique for loading data into part of a page without having to refresh the entire page
171
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
172
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest object
173
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
174
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototypal inheritance