JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data so we can access them in the future and multiple times

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

How do you declare a variable?

A

Using the variable keyword ‘var’

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

Using the assignment operator “=” after declaring the variable

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 (but cannot start with a number), dollar signs and 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

The same word with a capital letter would be different variable 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 save text content or series or 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 save for counting and calculating sums

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 save for when there are only two values “True & False” or “Yes & No”. Used for 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

It is an assignment operator. It assigns values to variables

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

To reassign it by using the declared 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 is an intentional absence of a value (var hello = null) whereas undefined is a variable that hasn’t been initialized yet (just declared ‘var hello;’)

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 can easily see what is being logged

Give five examples of JavaScript primitives. String, boolean, number, null, undefined

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

What data type is returned by an arithmetic operation?

A

An expression

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

What is string concatenation?

A

Joining two or more string values to create a new string

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

What purpose(s) does the + plus operator serve in JavaScript?

A

To add number values or to concatenate string values

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

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

A

Adds or concatenates the value of the right to the left of the operator and then assigns the result of the expression to the to the variable on the left

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

What are objects used for?

A

To group together a set of variables and functions around one subject

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

What are object properties?

A

A variable within an object

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

Describe object literal notation.

A

Define a new variable and assign it to an object literal using curly braces. Using property and values

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

How do you remove a property from an object?

A

Delete operator followed by dot notation object.property

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

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

A

Using the dot notation or the bracket notation

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

What are object methods?

A

A function within an object

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

What are arrays used for?

A

Arrays store a list of values that are related to each other

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

Describe array literal notation.

A

Define a new variable and assign it to an array literal notation using square brackets.

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

They are listed in a number index

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

.length - tells you how many properties are in an 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

Object.length - 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

Special type of object that can be called multiple times

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, optional name, zero or more parameters surrounded by parentheses, start of code block, optional return statement, closing 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

Function name with parentheses with or without arguments

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

Call is to run/pass the arguments. Function definition has a code block with ‘instructions’.

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

Parameter is what is listed in the function definition. Argument is the function call (actual values passed)

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

Why are function parameters useful?

A

Placeholder - to pass additional information into 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 causes the function to produce a value and prevents any more code in the function’s code block from being run

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

To check our progress and see if the outcome is what we are expecting or not

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

What is a method?

A

A function that is stored within 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 function can be called directly by its name, but a method is called using the dot notation

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

Using the method .pop() - it also returns that removed element

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

How do you remove the first element from an array?

A

Using the method .shift() - it also returns that removed element

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

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

A

Using the object & method Math.floor()

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

How do you generate a random number?

A

Using the object & method Math.random()

function getRandomNumberInRange(start, end) {
  var randomNum = Math.floor(math.random() * (end - start) + 1) + start;
  return randomNum;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

How do you delete an element from an array?

A

Using the method .splice()

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

How do you append an element to an array?

A

Using the method .push()

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

How do you break a string up into an array?

A

Using the method .split()

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

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

A

No. By console logging the variable of the original string.

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

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

A

37

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

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

A

No, we don’t always need to return.

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

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

A

39

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

How do you prepend an element to an array?

A

Using the method .unshift()

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

Give 6 examples of comparison operators.

A

Equal to, is not equal to, greater than, less than, strictly equal to, greater than or equal to

54
Q

What data type do comparison expressions evaluate to?

A

Boolean

55
Q

What is the purpose of an if statement?

A

If a condition is met - if it is a truthy

56
Q

Is else required in order to use an if statement?

A

No. We don’t have to use something else if it’s not true.

57
Q

Describe the syntax (structure) of an if statement

A

If keyword with condition in parentheses and the code block

58
Q

What are the three logical operators?

A

And operator (&&), Or operator (||), Not operator or bang (!)

59
Q

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

A

Put them in parentheses with a logical operator

60
Q

What is the purpose of a loop?

A

To allow us to repeat code

61
Q

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

A

To tell loop the start and stop point to run

62
Q

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

A

It represents the code in the curly brace that will be run - a single time the code block runs

63
Q

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

A

The beginning of each iteration, right before the code block is run

64
Q

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

A

Only once before anything. Tells us where to start

65
Q

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

A

Happens after the initialization and before each iteration before it continues

66
Q

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

A

After each iteration

67
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

68
Q

What does the ++ increment operator do?

A

Increments the variable by one

69
Q

How do you iterate through the keys of an object?

A

A for in loop

70
Q

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

A

Focus

71
Q

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

A

Blur

72
Q

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

A

Input

73
Q

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

A

Submit

74
Q

What does the event.preventDefault() method do?

A

Prevents the default action from being played out

75
Q

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

A

Refreshes page

76
Q

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

A

Elements - use it by typing it in the console ie. $form.elements

77
Q

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

A

Value property

78
Q

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

A

You won’t know where to look

79
Q

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

A

You can see what is being logged

80
Q

What is the event.target?

A

The target property returns the DOM element object that triggered the event

81
Q

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

A

It visually removes it from the page (from the document flow)

82
Q

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

A

It takes a css selector and returns true or false

83
Q

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

A

Using the .getAttribute()

84
Q

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

A

EVERY STEP

85
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

Create new event listener for every view

86
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

Write code for every situation (many else if statements)

87
Q

What is JSON?

A

Standard string format to transmit data in web applications

88
Q

What are serialization and deserialization?

A

Serialization is turning an Object into a stream of bytes. Deserialization is taking a stream of bytes and converting it back into an Object

89
Q

Why are serialization and deserialization useful?

A

To transfer data over a network

90
Q

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

A

Using JSON.stringify()

91
Q

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

A

Using JSON.parse()

92
Q

How do you store data in localStorage?

A

setItem(). First argument is the keyName and the second argument is Value. Both in quotes

93
Q

How do you retrieve data from localStorage?

A

Using .getItem()

94
Q

What data type can localStorage save in the browser?

A

Strings, but because of JSON, we can store arrays and objects as well

95
Q

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

A

Right before the window is closed or reloaded

96
Q

What is a method?

A

A function that is stored in a property of an object

97
Q

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

A

Definition has a code block. Method call is a dot notation and is being called with an object. Parameters vs arguments

98
Q

Describe method definition syntax (structure).

A

Function with function name being defined with none or more parameters. Opening code block and possible code inside. Or function being assigned to a property of an object.

99
Q

Describe method call syntax (structure).

A

Object, dot notation, method and parentheses with one or more arguments

100
Q

How is a method different from any other function?

A

It has to be attached to an object. Function is not associated with objects

101
Q

What is the defining characteristic of Object-Oriented Programming?

A

It is based on the concept of objects. Objects can contain both data (as properties) and behavior (as methods)

102
Q

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

A

Abstraction, encapsulation, inheritance, polymorphism

103
Q

What is “abstraction”?

A

It is simplifying complex things

104
Q

What does API stand for?

A

Application Programming Interface

105
Q

What is the purpose of an API?

A

Abstraction. A collection of tools created for someone else to use without needing to understand every piece that is happening

106
Q

What is this in JavaScript?

A

The object that it is in. It is a keyword

107
Q

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

A

It is available in a function’s code block even though it was never included in the function’s parameter list or declared with var

108
Q

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

A

Call time

109
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

It is nothing!

110
Q

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

A

It’s-a-me, Mario! The greet function is being called and this is being given a value

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

It’s-a-me, undefined! It’s referencing the window because there is no object hello() is attached to

112
Q

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

A

Unless you’re actively using the keyword, ‘this’ is nothing.

113
Q

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

A

The object the method is invoked on. Window for a general function

114
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypal inheritance

115
Q

What is a prototype in JavaScript?

A

An object that contains properties where we can create new things on

116
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

The prototype object has built in properties

117
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 in the object prototype

118
Q

What does the new operator do?

A

Allows a new object instance to be created. Make an empty object,adds a property to the new object that links to the constructor functions’ prototype object, binds the newly created object instance as the ‘this’ context and returns ‘this’

119
Q

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

A

Prototype

120
Q

What does the instanceof operator do?

A

Tests to see if the prototype property (constructor) occurs in the object

121
Q

What is a “callback” function?

A

Function passed into another function as an argument

122
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

Using the setTimeout() function

123
Q

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

A

Using setInterval()

124
Q

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

A

0

125
Q

What do setTimeout() and setInterval() return?

A

The return of setTimeout is a positive integer value which identifies the timer created by the call to setTimeout(). The return of setInterval is a numeric, non-zero value which identifies the timer created by the call to setInterval()

126
Q

What is AJAX?

A

A technique for loading data into part of a page without having to refresh the entire page

127
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

128
Q

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

A

XMLHttpRequest

129
Q

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

A

Load

130
Q

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

A

They both share an object in the prototypal chain - EventTarget