JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To use as an identifier in JavaScript and the variable must be identified with unique names.

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

How do you declare a variable?

A

Use var, let (variable with restricted scope), or const (a variable that can’t be reassigned) keyword.

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

Put variable (name) = (operator) and value. Ex) var a = 1.

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

The first character must start with a letter, underscore, or a dollar sign ($), and don’t forget variable names are case-sensitive and don’t use JavaScript’s reserved 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 any identifiers should always be typed with consistent character capitalization within acceptable rules.

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

It is used for storing and manipulating text in JavaScript.

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

It is a primitive wrapper object used to represent and manipulate 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

It is used to represent one of two values in a value, such as True or False, Yes or No, and On or Off.

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 performs some operation on single or multiple operands (data values) and produces a result.

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

Just update the new value under the same variable name or use temporary values.

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

They are equal in value (empty value) but different in type. Undefined can exist by itself in JS, but null is not. If you see the null object, that means your existing variable value is reserved for some reason.

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 identify easier, faster, and more directly when you work on some big project or work with coworkers.

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, and undefined. Everything else is an object.

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

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

It is the process of appending one string to the end of another 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

It produces the sum of numeric operands or string 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

It returns a boolean value.

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

It adds the value of the right operand to a 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

It holds multiple values in terms of properties and methods in an object.

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

What are object properties?

A

It’s a variable that is attached to the object. It is sorted as the same as an ordinary variable except for the attachment to the object.

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

Describe object literal notation.

A

var object name = {property name: value of it, another property name: value of it}.

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

Use delete keyword and the object name.property name or keyword delete object name[‘property name’].

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 name.property name = value that you want to update or object name[‘property name’] = value that you want to update.

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

What are arrays used for?

A

It is used when you store a collection of variables of the same type.

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

Describe array literal notation.

A

var array object name = [‘’, ‘’, ‘’ ];

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

How are arrays different from “plain” objects?

A

Objects represent a special data type that is mutable and can be used to store a collection of data. On the other hand, arrays are a special type of variable that is also mutable and can also be used to store a list of values.

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

What is the length property of an array?

A

The length property sets or returns the number of elements in that array.

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

How do you calculate the last index of an array?

A

the length of the array - 1.

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

What is a function in JavaScript?

A

It is a block of code also repeatable to perform a particular task or calculate a value.

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

Describe the parts of a function definition.

A

function keyword, named function (optional), parameter (optional, but it should take some input and return output and there is some obvious relationship between them.) inside of the parenthesis, curly brace for the function code block.

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

Describe the parts of a function call.

A

the function name, argument inside of the parenthesis.

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 definition’s meaning is to remember the method of it but stays still. On the other hand, a function call instructs JavaScript to execute the code of the function.

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

The parameters are listed inside the parenthesis. The arguments are the values received by the function when it is invoked.

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

Why are function parameters useful?

A

Because those are expected to be the same as variables except that the values of these variables are defined when we call the function.

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

It ends the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

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

It prints the output to the console, so we can see what is going on, and be able to fix the code if something is wrong.

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

What is a method?

A

It is a function that is a property of an object.

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 a function that belongs to an object.

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

Use array pop() method. name of array.pop();

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

Use Math.floor() function.

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

Use Math.random() 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

Use splice() method, pop() method, or shift() method (removes the first array).

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

Use unshift() method to add a new element at the beginning, push() method to add a new element at the end, or name of array[index number].

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

Use the 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. Use console.log() or go to read the documentation about it to check.

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

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

A

No. Use when it’s needed.

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

Give 6 examples of comparison operators.

A

Strict equal to (===), greater than (>), greater than or equal to (>=), less than or equal to (<=), less than (

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

What data type do comparison expressions evaluate to?

A

Boolean: true or false.

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

What is the purpose of an if statement?

A

To perform different actions based on different conditions. Use if statement to specify a code to be executed if a specified condition is true.

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

Is else required in order to use an if statement?

A

No, it’s not required to write the else part for the if statement.

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

Describe the syntax (structure) of an if statement.

A

Keyword if (variable name, comparison, value) { }.

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

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

A

Using logical operators (&&, ||, !).

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

What is the purpose of a loop?

A

It used to repeatedly run a block of code until a certain condition is met.

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

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

A

It is an expression that is evaluated once before every iteration.

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

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

A

It describes going through a set of operations that deal with the code conditions (Each time the code is running).

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

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

A

When the while loop is created.

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

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

A

Very first before anytime.

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

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

A

Right after the initialization.

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

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

A

at the end of each loop iteration.

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

63
Q

What does the ++ increment operator do?

A

Added 1 to the value of the variable.

64
Q

How do you iterate through the keys of an object?

A

Use the for in loop.

65
Q

What are four components of “the Cascade”.

A

Importance, source order, inheritance, and specificity.

66
Q

What does the term “source order” mean with respect to CSS?

A

The lower line on your style sheet is more powerful than the higher.

67
Q

How is it possible for the styles of an element to be applied to its children as well without an additional CSS rule?

A

Inheritance property.

68
Q

List the three selector types in order of increasing specificity.

A

Type selectors, class selectors, id selectors.

69
Q

Why is using !important considered bad practice?

A

Because it overrides any other declarations when this is used. Therefore, it breaks the natural cascading of the stylesheet, which makes it more difficult to troubleshoot when debugging.

70
Q

Why do we log things to the console?

A

To see easily and directly the code whenever I check, understand, and fix it.

71
Q

What is a “model”?

A

It’s a representation of something else.

72
Q

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

A

HTML.

73
Q

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

A

It refers to the JavaScript data.

74
Q

What is a DOM Tree?

A

Elements that include other elements, such as child elements.

75
Q

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

A

document.querySelector and document.getElementByID.

76
Q

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

A

querySelectorAll.

77
Q

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

A

If you are going to use that return value more than one time, it will help you to find it easily. Also, you don’t have to work again to get that return value.

78
Q

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

A

console.dir (directory).

79
Q

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

A

Because of the source order.

80
Q

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

A

It takes a string with the selector’s name (element, class, id, etc) and it returns their values.

81
Q

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

A

It takes a string with the selector’s name (element, class, id, etc) and it returns all of their values under the same name tag.

82
Q

What is the purpose of events and event handling?

A

To handle and verify inputs and actions (user input, user actions, and browser actions).

83
Q

What do [] square brackets mean in function and method syntax documentation?

A

It’s optional. But the type and listener must be there.

84
Q

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

A

EventTarget method addEventListener().

85
Q

What is a callback function?

A

It’s a function passed to another function as an argument.

86
Q

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

A

The first argument, which is the event target.

87
Q

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

A

The target property of the event. Ask MDN.

88
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick( ))

A

The first one is calling the function name handleClick means that function works when the click event is triggered. The second one, function name with parentheses means that it runs the code right away. It means, run this function when this page is loaded. However, in an event handler, it waits until the event triggers.

89
Q

What is the className property of element objects?

A

It gets and sets the value of the class attribute of the specified element.

90
Q

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

A

Using the getAttribute method.

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

92
Q

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

A

object name . textContent =

93
Q

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

A

No, because the event listener is not always needed.

94
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. I have to keep track

95
Q

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

A

Because it reduces the work if I set up the value of the thing to the variable.

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

97
Q

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

A

Refresh the page and refresh.

98
Q

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

A

Elements.

99
Q

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

A

Value property.

100
Q

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

A

Hard to debug when an error occurs.

101
Q

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

A

Checking and debugging very easily.

102
Q

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

A

No.

103
Q

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

A

appendChild() method. (ex: Parent.appendChild( ))

104
Q

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

A

Attribute, and value of that attribute.

105
Q

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

A

Create the element, add more attributes with setAttribute or textContent if needed, and choose the spot you want to insert the new element and use the appendChild() method to append it on there.

106
Q

What is the textContent property of an element object for?

A

Reading and writing.

107
Q

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

A

Use setAttribute() method or className as a property of the object.

108
Q

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

A

We can re-use it rather than work again and again to get that result.

109
Q

What is the event.target?

A

It is a reference to the object onto which the event was dispatched.

110
Q

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

A

Because of the default document flow which is the event bubbling. The event bubbling is a type of event propagation when the event first triggers on the innermost target element, and then successively triggers on the parents of the target element in the same nesting hierarchy until it reaches the very outermost DOM element or document object.

111
Q

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

A

event.target.tagName.

112
Q

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

A

The argument is a selector and it returns itself or the nearest matching ancestor.

113
Q

How can you remove an element from the DOM?

A

Using the .remove() method.

114
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

Using dom event.delegation.

115
Q

What is the event.target?

A

The target property of the event interface is a reference to the object onto which the event was dispatched. It holds the event trigger.

116
Q

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

A

It makes it invisible and it takes off from the document.

117
Q

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

A

It returns a Boolean value indicating whether an element is matched by a specific CSS selector or not.

118
Q

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

A

Use getAttribute() method.

119
Q

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

A

Whenever I set up the new variable or after creating a loop or condition.

120
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

I have to do it manually for each thing. It means I have to write an eventListener for each of them.

121
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

I have to write so many conditional statements for each of them.

122
Q

What is JSON?

A

JavaScript Object Notation. It’s a standard text-based format for representing structured data based on JavaScript object syntax. This is commonly used for transmitting data in web applications.

123
Q

What are serialization and deserialization?

A

Serialization takes an in-memory data structure and converts it into a series of bytes that can be stored and transferred over the network. Deserialization takes a series of bytes and converts it to an in-memory data structure that can be consumed programmatically.

124
Q

Why are serialization and deserialization useful?

A

To save the state of an object in order to be able to recreate it when needed.

125
Q

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

A

Using the JSON.stringify() method.

126
Q

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

A

Using the JSON.parse() method.

127
Q

How do you store data in localStorage?

A

Using the setItem() method.

128
Q

How do you retrieve data from localStorage?

A

Using the getItem() method.

129
Q

What data type can localStorage save in the browser?

A

Only strings.

130
Q

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

A

The event is fired when the window, the document, and its resources are about to be unloaded, like refresh or close the window.

131
Q

What is a method?

A

It’s a function that is a property of an object.

132
Q

How can you tell the difference between a method definition and a method call?

A

A method definition needs a function definition keyword with parameters followed by a function code block. On the other hand, a method call needs to put the property name of the object name with arguments.

133
Q

Describe method definition syntax (structure).

A

Put property name: function definition with parameter and opening curly brace for the function code block.

134
Q

Describe method call syntax (structure).

A

Put property name of the object name and put values in parentheses.

135
Q

How is a method different from any other function?

A

A function can pass the data that is operated and may return the data. The method operates the data contained in a class. In addition, a function can be called directly by its name because it lives on its own. On the other hand, a method is a function associated with an object property.

136
Q

What is the defining characteristic of Object-Oriented Programming?

A

It’s a programming paradigm based on the concept of objects that can contain data and behavior.

137
Q

What are the four “principles” of Object-Oriented Programming?

A

Abstraction, Encapsulation, Inheritance, Polymorphism.

138
Q

What is “abstraction”?

A

It’s one of the key concepts of OOP. The main goal is to handle complexity by hiding unnecessary details from the user.

139
Q

What does API stand for?

A

It’s the acronym Application Programming Interface, which is a software intermediary that allows two applications to talk to each other.

140
Q

What is the purpose of an API?

A

It simplifies programming by abstracting the underlying implementation and only exposing objects or actions the developer needs.

141
Q

What is “this” in JavaScript?

A

‘this’ references the current object. So, the left of the dot.

142
Q

What does it mean to say that this is an “implicit parameter”?

A

It’s an usable variable inside of a function even though it didn’t declare in that function.

143
Q

When is the value of “this” determined in a function; call time or definition time?

A

The value of this is determined at call time.

144
Q

Given the above character object, what is the result of the following code snippet? Why?
character.greet( );

A

It will get the value of the variable message which is It’s me, Mario!. Because that is a value of the greet property of the character object (left of the dot).

145
Q
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello( );
A

It will get undefined because there is no function definition named hello.

146
Q

How can you tell what the value of this will be for a particular function or method definition?

A

I can’t

147
Q

How can you tell what the value of this is for a particular function or method call?

A

Look left of the dot.

148
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypal (prototype-based) inheritance.

149
Q

What is a prototype in JavaScript?

A

It’s an object that contains properties and methods that can be used by other objects.

150
Q

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?

A

Because of the prototype chain. It explains why different objects have properties and methods defined on other objects available to them.

151
Q

If an object does not have it’s own property or method by a given key, where does JavaScript look for it?

A

Look it up to the proto objects.