JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Variables are used to store data that can be referenced and/or changed later on.

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

How do you declare a variable?

A

You can declare a variable using var (global scoped) or let, const (block scoped).

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

You assign a value to a variable using =

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

Variables must start with a letter, underscore, or $ sign. You may not use any JavaScript keywords.

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

This means that the variable names must always be typed with consisted capitalization of characters. Otherwise, they will be treated as different variables entirely.

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

A string is used to store and manipulate 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

JavaScript numbers are used to store numerical values as double precision floating point 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

Boolean variables are used to store a value of true or false, and are often used to evaluate the truth of an expression.

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 is the assignment operator that is used to store the operand on the right side to the operand on the left side.

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

There are various ways to update the value of a variable. Besides the assignment operator, we can use +=, -=, *=. /=

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 that means nothing. Undefined means that the variable has been declared but not defined yet.

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 is a good practice to include labels as it allows the developer to know exactly context of the data being displayed.

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

Give five examples of JavaScript primitives.

A

Undefined, null, boolean, number, string

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

If both operands are numerical data, the operation will result in numerical data. However, if an operand is a string, then operation will result in a string.

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

What is string concatenation?

A

The arithmetic operation of two string operands.

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

The + operator is used to add operands on the left and right side of the operator.

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

The += operator adds the value of the right operand with the left operand and stores the result in the left operand.

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

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

A

The += operator adds the value of the right operand with the left operand and stores the result in the left operand.

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

What are objects used for?

A

An object is a self-contained component with its own properties, functions, and data structures. They are used to encapsulate their own actions from the rest of the program.

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

What are object properties?

A

Properties are values associated with a JavaScript object. Properties are in the form of key:value

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

Describe object literal notation.

A

The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array.

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

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

A

By using the dot nation or bracket notation

myCar.make
myCar[‘make’]

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

What are arrays used for?

A

An array allows for a collection of multiple items under a single variable name.

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

Describe array literal notation.

A

An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ( [] ).

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

How are arrays different from “plain” objects?

A

An array is used to store multiple items under a single variable. An object is used to contain self-related items under a single object name.

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

What number represents the first index of an array?

A

0

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

What is the length property of an array?

A

The amount of items stored in the array.

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

How do you calculate the last index of an array?

A

By placing length-1 in the index.

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

What is a function in JavaScript?

A

A function is a block of instructions that accomplished a certain task (described by the name of the function).

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

Describe the parts of a function definition.

A
function keyword
optional name
opening curly bracket
code block
return value
closing curly bracket
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

A function call causes the function code to execute. A function definition simply lays out the steps but doesn’t cause code to execute.

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

What is the difference between a parameter and an argument?

A

A parameter is used as an input in a function definition. An argument is used as input in a function call.

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

Why are function parameters useful?

A

They allow the function to be reusable with a variety of arguments.

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

What two effects does a return statement have on the behavior of a function?

A

The return statement ends the execution of the function and returns the value to the caller.

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

Why do we log things to the console?

A

We log to the console so we know what the code is doing. This ensures that the code is functioning as intended.

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

What is a method?

A

A method is a property of an object that contains a function definition.

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

How is a method different from any other function?

A

A method is associated with an object while a function is not.

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

How do you remove the last element from an array?

A

pop() method

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

How do you round a number down to the nearest integer?

A

You use the Math.floor() method.

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

How do you generate a random number?

A

Math.method() function

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

How do you delete an element from an array?

A

splice() method

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

How do you append an element to an array?

A

push() method

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

How do you break a string up into an array?

A

split() method

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

Do string methods change the original string? How would you check if you weren’t sure?

A

No, they

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

Roughly how many string methods are there according to the MDN Web docs?

A

According to MDN, there are roughly 50 string methods.

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

Is the return value of a function or method useful in every situation?

A

No, it certain situations, the return value is irrelevant to the functionality.

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

Roughly how many array methods are there according to the MDN Web docs?

A

There are roughly 41 array methods.

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

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

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

Give 6 examples of comparison operators.

A

===, !==, <, >, <=, >=

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

What data type do comparison expressions evaluate to?

A

Boolean

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

What is the purpose of an if statement?

A

To execute code if a condition is met.

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

Is else required in order to use an if statement?

A

No, not all if statements need an else statement.

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

Describe the syntax (structure) of an if statement.

A
if  conditional keyword
(........)  conditional expression
{  opening curly brace
code block
}   closing code block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

What are the three logical operators?

A

&& (AND), || (OR), ! (NOT)

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

How do you compare two different expressions in the same condition?

A

By using a logical operator to connect the two different expressions.

58
Q

What is the purpose of a loop?

A

Loops exist to run repeated code until a condition is met. This prevent us from having to rewrite many lines of code and allow us to dynamically set the number of repetitions.

59
Q

What is the purpose of a condition expression in a loop?

A

The condition expression is tested each time the loop repeats. If the condition evaluates to false, the loop exits.

60
Q

What does “iteration” mean in the context of loops?

A

Iteration is the process of repeating steps

61
Q

When does the condition expression of a while loop get evaluated?

A

The conditional expression is evaluated first before the statements inside the the loop execute.

62
Q

When does the initialization expression of a for loop get evaluated?

A

The initialization expression for a for loop comes first.

63
Q

When does the condition expression of a for loop get evaluated?

A

The condition expression is evaluated at the end of every loop iteration.

64
Q

When does the final expression of a for loop get evaluated?

A

The final evaluation is the end of the loop. It is the evaluation that determines to end the loop as condition returns false.

65
Q

Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

break

66
Q

What does the ++ increment operator do?

A

In increments a variable’s value by 1.

67
Q

How do you iterate through the keys of an object?

A

By utilizing a for…in loop to traverse each property of the object.

68
Q

Why do we log things to the console?

A
69
Q

What is a “model”?

A
70
Q

Which “document” is being referred to in the phrase Document Object Model?

A
71
Q

What is the word “object” referring to in the phrase Document Object Model?

A
72
Q

What is a DOM Tree?

A
73
Q

Give two examples of document methods that retrieve a single element from the DOM.

A
74
Q

Give one example of a document method that retrieves multiple elements from the DOM at once.

A
75
Q

Why might you want to assign the return value of a DOM query to a variable?

A
76
Q

What console method allows you to inspect the properties of a DOM element object?

A
77
Q

Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A
78
Q

What does document.querySelector() take as its argument and what does it return?

A
79
Q

What does document.querySelectorAll() take as its argument and what does it return?

A
80
Q

Why do we log things to the console?

A
81
Q

What is the purpose of events and event handling?

A
82
Q

Are all possible parameters required to use a JavaScript method or function?

A
83
Q

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A
84
Q

What is a callback function?

A
85
Q

What object is passed into an event listener callback when the event fires?

A
86
Q

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A
87
Q

What is the difference between these two snippets of code?

A
88
Q

What is the className property of element objects?

A

The className property of the Element interface gets and sets the value of the class attribute of the specified element.

89
Q

How do you update the CSS class attribute of an element using JavaScript?

A

You can utilize the setAttribute method or the .className method

90
Q

What is the textContent property of element objects?

A

The textContent property of the Node interface represents the text content of the node and its descendants.

91
Q

How do you update the text within an element using JavaScript?

A

Node.textContent

92
Q

Is the event parameter of an event listener callback always useful?

A

Not always. The event parameter is not always used in the event handler function.

93
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

More complicated as we would have no way to keep track of the amount of times the button was clicked.

94
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

Storing information in variables allows you to dynamically alter the content of the website.

95
Q

What event is fired when a user places their cursor in a form control?

A

mouseover event

96
Q

What event is fired when a user’s cursor leaves a form control?

A

Blur event

97
Q

What event is fired as a user changes the value of a form control?

A

Input event

98
Q

What event is fired when a user clicks the “submit” button within a ?

A

The submit event

99
Q

What does the event.preventDefault() method do?

A

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

100
Q

What does submitting a form without event.preventDefault() do?

A

It causes the page to refresh after the form is submitted.

101
Q

What property of a form element object contains all of the form’s controls.

A

HTMLFormElement.elements

102
Q

What property of a form control object gets and sets its value?

A

HTMLFormElement.elements

103
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

You may have made a mistake early on that affects every step thereafter. It’s best to check your code through each step in the terminal.

104
Q

What is an advantage of having your console open when writing a JavaScript program?

A

You can follow along with the code to see how the values stored in variables change as you progress through your code.

105
Q

Does the document.createElement() method insert a new element into the page?

A

No, it simply creates an element node that will need to be appended by the programmer.

106
Q

How do you add an element as a child to another element?

A

The appendChild method of the Node interface

107
Q

What do you pass as the arguments to the element.setAttribute() method?

A

The attribute and the value of the attribute.

108
Q

What steps do you need to take in order to insert a new element into the page?

A

First, you must querySelect the parent element to append to. Then you must utilize the createElement method of the Document object with an element specified as an argument. The return of the that express is stored in a variable. The variable is then appended as a Child to the parent element.

109
Q

What is the textContent property of an element object for?

A

The text that is displayed to the user when viewing the element.

110
Q

Name two ways to set the class attribute of a DOM element.

A

.className = value

setAttribute(‘class’, value)

111
Q

What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A

They allow you to dynamically render content rather than rely on static predefined HTML

112
Q

Give two examples of media features that you can query in an @media rule.

A

min-width, max-width

113
Q

Which HTML meta tag is used in mobile-responsive web pages?

A

HTML Viewport meta tag

114
Q

What is the event.target?

A

The target is the property of an event that references the element upon which the event was fired.

115
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A

By default, the events bubble up the DOM tree unless specified not to.

116
Q

What DOM element property tells you what type of element it is?

A

element.tagName

117
Q

What does the element.closest() method take as its argument and what does it return?

A

The element.closest() method takes in an element as its argument and returns the first ancestor of the argument.

118
Q

How can you remove an element from the DOM?

A

remove() method of the element

119
Q

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?

A

Assuming that the new clickable DOM elements share the same parent, you can utilize bubble-up by applying a selector for the inner element and using the closest() method to select the parent. Then any new DOM elements will have the event listener applied to them.

120
Q

What is the affect of setting an element to display: none?

A

It causes the element (and it’s descendants) to not be rendered and not affect document flow. It acts as if the element does not exist at all.

121
Q

What does the element.matches() method take as an argument and what does it return?

A

It takes in a specified CSS selector and returns a boolean value.

122
Q

How can you retrieve the value of an element’s attribute?

A

You can utilize the getAttriibute() method which returns of the value of an element’s attribute in the form of a string.

123
Q

At what steps of the solution would it be helpful to log things to the console?

A

At the start of every implementation to ensure we are accessing the correct data.

124
Q

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?

A

You would need to manually query and append the tab and view rather than utilize the reusable pattern with event delegation.

125
Q

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?

A

You would need to utilize a nested conditional statement which would we inefficient.

126
Q

What is JSON?

A

JSON is a data representation of a JavaScript object in string format.

127
Q

What are serialization and deserialization?

A

Serialization refers to the process of converting a JavaScript object to string format known as JSON. Deserialization is the process of converting the JSON object to JavaScript.

128
Q

Why are serialization and deserialization useful?

A

Serialization allows us to convert data to string format for storage and data transmission while deserialization allows us to recreate the objects from JSON.

129
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

JSON.stringify()

130
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

JSON.parse()

131
Q

How do you store data in localStorage?

A

setItem() method of the localStorage object

132
Q

How do you retrieve data from localStorage?

A

getItem() method of the localStorage object

133
Q

What data type can localStorage save in the browser?

A

JSON

134
Q

When does the ‘beforeunload’ event fire on the window object?

A

When the window is closed or reloaded.

135
Q

What is a code block

A
136
Q

What does block scope mean

A

We cannot access the variable declared within a block when we are outside that block.

137
Q

What is the scope of a variable declared with const or let?

A

block

138
Q

What is the difference between let and const?

A

Let can be reassigned while cosnt is read only.

139
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A

We are not reassigning the value of the variable since it is still referencing the same position in memory.

140
Q

How should you decide on which type of declaration to use?

A

If you plan on changing the value of the variable, use let. Otherwise, use const.