JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Allows you to store data and access them later.

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

How do you declare a variable?

A
  1. variable keyword
  2. variable name
  3. assignment operator (=)
  4. variable value
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

=

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, digits, _, $, and it cannot start with a number
(ex: cat1 is okay but not 1cat)

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

uppercase and lowercase matters in JavaScript.
(ex. JavaScript != javascript)
precision is mandatory

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

Storing some sequence of 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

Stored when we need to manipulate mathematical information

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 important for conditions. Able to represent if something is or isn’t.
Their purpose is 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

It is the assignment operator.
Used to assign a value to a variable.

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

When updating, you do not need the variable keyword, but still need the assignment operator.
+=

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

undefined ==> a value that is not actually there. Means empty
null ==> null is a value that is assigned. It is done on purpose.

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

Having labels makes it clear what we are working with.
Sometimes, there are multiple console.logs and you would need to differentiate.

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

Give five examples of JavaScript primitives.

A
  1. String
  2. Numbers
  3. Booleans
  4. Null
  5. 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

a number

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

What is string concatenation?

A

The process of joining together two or more strings to create one new string. Uses the + (plus operator)

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 plus operator can be used for string concatenation and as an arithmetic operation (adding).

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

A boolean
(true or false)

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 += is an assignment operator that sums up the left and right operand values then assign the obtaining result to the left operand.
It will increment your sum variable with the amount next to it.

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

What are objects used for?

A

Objects group together a set of variables and functions to create a model of something you would recognize from the real world

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

What are object properties?

A

If a variable is part of an object, it is called a property. Properties tell us about the object, such as the name of a hotel or the number of rooms it has.

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

Describe object literal notation.

A

The object is the curly braces and their contents. Separate each key from its value using a colon. Separate each property and method with a comma (but not after the last value).

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 the delete operator and assign the property using either the dot or bracket notation.

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 and bracket notation.
Ex. student.firstName || student[‘firstName’]

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.
You should consider using an array whenever you are working with a list or a set 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

The values are assigned to the array inside a pair of square brackets[ ], and each value is separated by a comma.

The values in the array do not need to be the same data type, so you can store a string, a number and a Boolean all in the sam array.

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

Arrays are a special type of object. They hold a related set of key/value pairs (like all objects), but the key for each value of an array is its index number.
They tell you the length. To add data, you would need a method (push). Arrays have an order.

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

Each array has a property called length, which holds the number of items in the array.
( ex. array.length)

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

You take the length property of the array and subtract by 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

Packed up code that can be reused throughout a program.
A set of statements that performs a task or calculates 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
  1. The function keyword to begin the creation of the function
  2. An optional name
  3. A comma-separated list of zero or more parameters, surrounded by ( )
  4. The start of the function’s code block, indicated by an { opening curly
    brace
  5. An optional return statement.
  6. The end of the function’s code block, as indicated by a } closing curly brace
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
  1. The function’s name
  2. A comma-separated list of zero or more arguments surrounded by ( )
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

Function call is just the function name with the argument.
Function definition has the function keyword and the code block.

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 a placeholder. It is a variable whose value is not known until we call the function and pass an argument. When the function’s code block is run, the parameter will be holding the value of the argument.

When we define a function, we declare parameters
When we call a function, we pass it arguments

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

Why are function parameters useful?

A

It can be used as a placeholder for when the arguments will eventually be passed.
Also, the parameter can be used within the code block when making 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
  1. Causes the function to produce a value we can use in our program
  2. 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

The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code.

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

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

Using the pop() method
Array.prototype.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

using the floor method of the Math object.
Math.floor()

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

by calling the random() method of the Math object.
Math.random()

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

By calling the splice() method
Array.prototype.splice()
splice(start, deleteCount)

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

By calling the push() methods.
Array.prototype.push()
append = Put it on the end

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

By calling the split() method
String.prototype.split()

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. Strings are immutable.
You would check logging the value to the console.

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

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. functions and methods are only useful in specific situations

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

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
  1. Less than (<)
  2. Greater than (>)
  3. Less than or equal too (<=)
  4. Greater than or equal too (>=)
  5. Strict equality operator (===)
  6. Strict inequality operator (!==)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What data type do comparison expressions evaluate too?

A

Booleans

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 make decisions in the code

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

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
  1. if keyword
  2. condition
  3. opening curly brace
  4. code to execute if value is true
  5. closing curly brace
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

What are the three logical operators?

A
  1. Logical AND (&&)
  2. Logical OR (||)
  3. Logical 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

You use the logical operators (AND, OR)
&&, ||

58
Q

What is the purpose of a loop?

A

Loops are a programming element that repeat a portion of code a set number of times until the desired process is complete.

59
Q

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

A

If the value of conditionExpression is true, the loop statements execute. Otherwise, the for loop terminates.

60
Q

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

A

Iteration is the process of repeating steps.
How many times the loop gets repeated.

61
Q

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

A

The condition test occurs before statement in the loop is executed.
Occurs before each iteration

62
Q

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

A

One time before everything.

63
Q

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

A

condition expression is evaluated after the initialization expression, and then it happens before the final expression after each iteration.

64
Q

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

A

Before the condition and after the code block

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

Adds one to the variable it is applied too and saves it in that variable

67
Q

How do you iterate through the keys of an object?

A

Use a for in loop

68
Q

Why do we log things to the console?

A

So that we can see all the properties within the object
Helps with debugging.

69
Q

What is a “model”?

A

A series of objects that represents a web page that the browser saves in memory.
A model of the HTML webpage

70
Q

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

A

The HTML Document

71
Q

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

A

A javascript object that is modeling the HTML document

72
Q

What is a DOM Tree?

A

It is a representation of the entire webpage. Every element, attribute, and piece of text in the HTML is represented by its own DOM node.
Everything below the document node is called the DOM Tree.

73
Q

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

A
  1. querySelector()
  2. getElementById()
74
Q

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

A

querySelectorAll()

75
Q

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

A

You can use dot notation to access the content within that node since the DOM is made up of all objects

76
Q

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

A

console.dir()
dir = directory

77
Q

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

A

It may slow down the page rendering speed since it will execute the javascript code on the spot.
HTML document loads from top to bottom. Part of HTML will not be created yet if you are using JavaScript to work with the DOM

78
Q

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

A

document.querySelector() takes in string as an argument and returns the first element that matches that selector.

79
Q

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

A

document.querySelectorAll() takes in a string as an argument and returns returns a nodeList object of all the elements that the string represents.

80
Q

Why do we log things to the console?

A

Helps with debugging and checking to see if there are any mistakes in our code

81
Q

What is the purpose of events and event handling?

A

Allows users to interact with the webpage.
For us to be aware when a certain action happens. Our job as developers is to have something happen when that action happens

82
Q

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

A

No

83
Q

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

A

addEventListener()

84
Q

What is a callback function?

A

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

85
Q

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

A

A function that we have made will be called when the event fires
The event object.

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

The event.target is the piece of HTML where the event originated.
We can check by logging it into the console.
We can get more information by googling online using MDN

87
Q

What is the difference between these two snippets of code?
1. element.addEventListener(‘click’, handleClick)
2. element.addEventListener(‘click’, handleClick())

A

In the first snippet of code, the handleClick function is being called when the click event happens.
In the second snippet of code, the handleClick function is being called when the page loads.

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.
Property containing that class attribute of that element.

89
Q

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

A

Using the className property

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

Using the textContent property using the assignment operator

92
Q

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

A

No

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 since a variable can be reused

94
Q

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

A

Because variables are shorter to write and you can use them whenever you need to.
Better to use data from the language you are using.

95
Q

What is JSON?

A

JavaScript Object Notation (JSON)
It is a standard text-based format for representing structured data based on JavaScript object syntax

96
Q

What are serialization and deserialization?

A

Serialization: the process of turning an object in memory into a stream(one line) of bytes so you can do stuff like store it on disk or send it over the network.

Deserialization: is the reverse process: turning a stream of bytes into an object in memory.

97
Q

Why are serialization and deserialization useful?

A

Serialization turns the object into a string, which is useful for when you want to transmit data across a network. Although serialized data resembles javascript object literal syntax, it can be used independently from javascript.

Deserialization is useful because it can take serialized data and return it into object form.

98
Q

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

A

Use the JSON.stringify() method

99
Q

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

A

Use the JSON.parse() method

100
Q

How do you store data in localStorage?

A

setItem method of the local storage object

101
Q

How do you retrieve data from localStorage?

A

getItem method of the localStorage object

102
Q

What data type can localStorage save in the browser?

A

A string

103
Q

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

A

When the window is about to be closed

104
Q

What is a method?

A

A method is a function which is a property of an object.

105
Q

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

A

Method definition: Includes a property name, the function key word, parameters, and function code block.
Method call: Only includes the object name with the method using dot notation and the arguments inside parentheses.

106
Q

Describe method definition syntax (structure).

A

Method definition: Define an object literal. Within the object literal, includes a property name being define. Within the value is the function key word, parameters inside parentheses, and the function code block.
ex. var calculator = {
add: function (x, y) {
return x + y;
}

107
Q

Describe method call syntax (structure).

A

Method call: Only includes the object name with the method using dot notation and the arguments inside parentheses.
ex. calculator. add(5, 5);

108
Q

How is a method different from any other function?

A

A method is a function that is ONLY the property of an object

109
Q

What is the defining characteristic of Object-Oriented Programming?

A

Objects can contain both data (as properties) and behavior (as methods).

110
Q

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

A
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
111
Q

What is “abstraction”?

A

The most important part of the term “abstraction” boils down to being able to work with (possibly) complex things in simple ways.
Light switch analogy.

112
Q

What does API stand for?

A

Application Programming Interface (API)

113
Q

What is the purpose of an API?

A

The purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

114
Q

What is this in JavaScript?

A

Implicit parameter that allows up to call a variable within the code block

115
Q

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

A

A value that is just given to each function. It is always present.
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.

116
Q

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

A

Call time

117
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

The value of this is nothing because the greet method has not been called yet.

118
Q

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

A

We wouldn’t know what the value of this is when the method is being defined. You would only know when the method is called. ‘This’ is a placeholder and we never know the value of the placeholder.

119
Q

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

A

Whatever is on the left of the dot.

120
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype/prototypal-based inheritance

121
Q

What is a prototype in JavaScript?

A

The prototype is an object that is associated with every functions and objects by default in JavaScript

122
Q

How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?

A

It turns out arrays don’t actually have the methods that you’ve come to know. Instead, those methods are defined on a “prototype” object and arrays simply borrow those methods when they’re needed.

123
Q

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

A

In the prototype object

124
Q

What does the new operator do?

A

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

125
Q

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

A

The prototype property

126
Q

What does the instanceof operator do?

A

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.

127
Q

What is a “callback” function?

A

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

128
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

Use the setTimeout method

129
Q

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

A

Use the setInterval method

130
Q

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

A

default time delay of 0

131
Q

What do setTimeout() and setInterval() return?

A

setInterval returns the intervalID and setTimeout returns the timeoutID. Which will always be a positive integer

132
Q

What is AJAX?

A

Ajax, which initially stood for Asynchronous JavaScript And XML, is a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest.
Loading data to a part of the page without a need for reloading.

133
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

134
Q

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

A

XMLHttpRequest()

135
Q

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

A

The ‘load’ event is fired when an XMLHttpRequest transaction completes successfully.

136
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 share a prototype

137
Q

What is Array.prototype.filter useful for?

A

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

138
Q

What must the return value of myFunction be if the following expression is possible?
myFunction()();

A

It would return the return value of the function that is inside of myFunction.

139
Q

What does this code do?
const wrap = value => () => value;

A

When wrap is called, it would return the function that holds value

140
Q

In JavaScript, when is a function’s scope determined; when it is called or when it is defined?

A

When it is defined

141
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

Closures

142
Q

What are closures?

A

It is a backpack for functions that remember values that it might need later.
If the value of the outer function is used in the inner function. The value in the outer function is remembered when the value is used in the inner function.