JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Store results, quantities, conditionals

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

How do you declare a variable?

A

Keyword var and variable 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

Use ‘=’

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

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

lowercase names are not the same as capitalizes names

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

math and quantities

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

conditionals

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

What does the = operator mean in JavaScript?

A

Assigning value

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

How do you update the value of a variable?

A

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

Different data types. Null always needs to be assigned.

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

Why is it a good habit to include “labels” when you log values to the browser console?

A

So you know what the logged values are for.

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

Give five examples of JavaScript primitives.

A

boolean, number, string, 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

Appending strings to make 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

Adding numbers, concatenating 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/appends and assigns the new value

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

What are objects used for?

A

Store related values

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

What are object properties?

A

Keys to access object’s values

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

Describe object literal notation.

A

Curly braces + key, value pairs

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

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 brackets

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

Why do we log things to the console?

A

To check for expected output of functions and objects

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

What is a method?

A

Function of an object

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

How is a method different from any other function?

A

Function is stand-alone. Methods belong to objects and are called by first referencing the object.

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

How do you remove the last element from an array?

A

pop

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

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

A

floor()

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

How do you generate a random number?

A

random()

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

How do you delete an element from an array?

A

pop, shift, splice

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

How do you append an element to an array?

A

push

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

How do you break a string up into an array?

A

split

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

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

A

No, strings are immutable. Check MDN.

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

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

A

Several dozen

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

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

A

No

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

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

A

Several dozen

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

Give 6 examples of comparison operators.

A

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

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

What is the purpose of an if statement?

A

To check a condition

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

Is else required in order to use an if statement?

A

No

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

Describe the syntax (structure) of an if statement.

A

If { /* code */ }

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

What are the three logical operators?

A

&&, ||, !

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

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

A

Use &

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

What is the purpose of a loop?

A

To repeat code as many times as needed

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

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

A

Determines whether to continue the loop

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

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

A

Each cycle of the loop

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

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

A

Before each iteration

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

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

A

Beginning of the loop

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

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

A

Before each iteration

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

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

A

After the code block is run.

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

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

What does the ++ increment operator do?

A

Increments the variable it’s called on by one

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

How do you iterate through the keys of an object?

A

for key in object

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

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

A

focus

56
Q

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

A

blur

57
Q

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

A

input

58
Q

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

A

submit

59
Q

What does the event.preventDefault() method do?

A

Prevents the default action

60
Q

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

A

Data get submitted to server. Form resets. Page reloads.

61
Q

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

A

HTMLFormControlCollection

62
Q

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

A

value

63
Q

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

A

You could have more bugs.

64
Q

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

A

You can see errors.

65
Q

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

A

no

66
Q

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

A

appendChild()

67
Q

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

A

name, value

68
Q

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

A

You need to get a node from DOM where you want to insert the new element and use append or appendChild

69
Q

What is the textContent property of an element object for?

A

Stores the text value

70
Q

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

A

className = name or

setAttribute(‘class’, name);

71
Q

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

A

You can reuse it. You can add it as a callback function when an event occurs.

72
Q

What is the event.target?

A

The element that the event originated from.

73
Q

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

A

event bubble up

74
Q

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

A

tagName

75
Q

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

A

It takes a selector and returns the closest ancestor that matches the selector.

76
Q

How can you remove an element from the DOM?

A

remove()

77
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

Add an eventListener() to the parent and have the callbackfunction work with the event.target.

78
Q

What is the event.target?

A

The element where the event originated from

79
Q

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

A

It does not display

80
Q

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

A

Check is the element matches the selector passed in.

81
Q

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

A

getAttribute()

82
Q

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

A

So you can make sure your variables have expected data

83
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 add a listener for each tab.

84
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 have a condition block for each tab.

85
Q

What is JSON?

A

Text-based data format following JavaScript object format

86
Q

What are serialization and deserialization?

A

Serialization is the process of turning an object into a series of bytes. Deserialization is turning a series of bytes into an object the program can work with.

87
Q

Why are serialization and deserialization useful?

A

Serialization is necessary to send data across the network or store data in a compact flat file. Deserialization is necessary to take serialized bytes and output objects that a program can interact with.

88
Q

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

A

JSON.stringify()

89
Q

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

A

JSON.parse()

90
Q

How do you store data in localStorage?

A

setItem(key, value)

91
Q

How to you retrieve data from localStorage?

A

getItem(key)

92
Q

What data type can localStorage save in the browser?

A

primitive data types

93
Q

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

A

When the page is refreshed or if the user is leaving the page

94
Q

What is a method?

A

function of an object

95
Q

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

A

Method definition has a code block. Method call has arguments inside parentheses or just parentheses if there’s no arguments.

96
Q

Describe method definition syntax (structure).

A

Method name : function definition block

97
Q

Describe method call syntax (structure).

A

obj.method()

98
Q

How is a method different from any other function?

A

method is attached to an object

99
Q

What is the defining characteristic of Object-Oriented Programming?

A

Pairs variables and functions in one structure

100
Q

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

A

abstraction, encapsulation, inheritance, polymorphism

101
Q

What is “abstraction”?

A

Separates functionality from implementation. You can use an object and its methods without knowing how it works.

102
Q

What does API stand for?

A

application programming interface

103
Q

What is the purpose of an API?

A

Provides a way from different programs/systems/applications to communicate with each other.

104
Q

What is this in JavaScript?

A

An implicit parameter to the object the method is being called on

105
Q

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

A

It is not explicitly defined in the parameters

106
Q

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

A

call time

107
Q
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);
  }
};
A

This references nothing until the function is called.

108
Q

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

A

It-s-a-me Mario! Because this references the character variable.

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

Undefined. This references the default Window object, which doesn’t have firstName variable

110
Q

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

A

You can’t know until the function is called.

111
Q

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

A

Look at the object to the left of the dot.

112
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypal

113
Q

What is a prototype in JavaScript?

A

A template that objects can inherit properties from and build off of.

114
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

These methods are inherited from the prototype.

115
Q

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

A

It looks for it in the prototype set in __prototype__ property.

116
Q

What is a “callback” function?

A

Passing a function as a value, so it can be used when an event triggers.

117
Q

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?

A

setTimeout

118
Q

How can you set up a function to be called repeatedly without using a loop?

A

setInterval

119
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0

120
Q

What do setTimeout() and setInterval() return?

A

intervalID

121
Q

What does the new operator do?

A

Instantiates a new object

122
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

prototype

123
Q

What is a client?

A

Client makes a request for data such as a web page or list of information

124
Q

What is a server?

A

Server sends a response to a request

125
Q

Which HTTP method does a browser issue to a web server when you visit a URL?

A

GET

126
Q

What three things are on the start-line of an HTTP request message?

A

HTTP, request type, version of protocol

127
Q

What three things are on the start-line of an HTTP response message?

A

HTTP, version of protocol, status code

128
Q

Where would you go if you wanted to learn more about a specific HTTP Header?

A

MDN

129
Q

Is a body required for a valid HTTP request or response message?

A

no

130
Q

What is AJAX?

A

A way to send and retrieve data from a server

131
Q

What does the AJAX acronym stand for?

A

Asynchronous Java & XML

132
Q

Which object is built into the browser for making HTTP requests in JavaScript?

A

XMLHttpRequest

133
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

load

134
Q

An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

They’re both event objects

135
Q

What is Array.prototype.filter useful for?

A

To filter results by an expression

136
Q

What is Array.prototype.map useful for?

A

Apply an operation to all elements of the array

137
Q

What is Array.prototype.reduce useful for?

A

Combine all elements of the array into one value