JavaScript Flashcards

1
Q

What is the purpose of variables?

A

It allows the program to store values

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

How do you declare a variable?

A

Begin with the keyword var followed by the variable’s 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 “=” to set the variable equal to the value

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

Any letters, numbers, “$”, and “_”

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

Casing changes the name of a variable

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 represent text values

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 represent numerical values used in calculation and indexing

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 represent logic values

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 means to assign a 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 the variable with the 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

Null is still considered an object while undefined is not

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 gives more clarity to the console, which is helpful for debugging

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

When two or more strings are combined to make a new string that is a combination of the original strings

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 is both the arithmetic operator for addition and the concatenation operator for 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

It applies the + operator to the values on either side of the += and assigns the result to the variable on the left

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

What are objects used for?

A

They group together variables and functions to create a model of a real-world entity

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

What are object properties?

A

An object’s variables; a key/value pair

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

Describe object literal notation.

A

You define the object in curly braces {}, separate properties with commas, and define key/value pairs using a colon to separate them

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 keyword delete followed by the object

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 object.key or bracket notation object[‘key’]

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

What are arrays used for?

A

Arrays are used for listing items

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

Describe array literal notation.

A

Any number of values separated by commas, enclosed in 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

The name of a property in the array is preset as a number

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

Gives the total number of objects 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

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

A portion of code that can be reused throughout a program

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

the keyword “function”, the optional function name, the parameters, and the 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

There is the function name followed by the arguments within the parentheses

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 does not have the function keyword or 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 the the general input used when defining the function, while an argument is the specific input used when calling the function

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 take in values that can be manipulated across various cases

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 provides a value that the function evaluates to, and terminates the reading of the function code block

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 verify our code works and help debug our code

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

What is a method?

A

A function defined within 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

They can only be called on the object they are defined in

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

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

Math.floor(number)

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

.splice(start, deleteCount, item1, item2, …)

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(element)

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(separator)

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

They do not; this can be confirmed by console logging the original string

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

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, for example .push() will return the new length of the array

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

~30

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

===, !==, >=, <=, >, less than sign

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 write code that only executes conditionally

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

keyword if, condition statement, 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

&&, ||, !

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

Connect them with a logical operator

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

What is the purpose of a loop?

A

It allows us to repeat behavior

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

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

A

It tells the loop whether or not to execute

60
Q

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

A

Each time the loop goes over the code is called an iteration

61
Q

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

A

At the beginning of each iteration

62
Q

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

A

Once, at the start of the for loop

63
Q

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

A

At the start of each iteration of the for loop

64
Q

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

A

At the end of each iteration of the for loop

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

It evaluates the expression (variable + 1) and assign the result to the variable

67
Q

How do you iterate through the keys of an object?

A

Use a for…in loop

68
Q

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

A

It is harder to pinpoint where a bug has occurred, since there is more code to consider

69
Q

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

A

You can log checkpoints in your code to the console to make sure everything is working properly

70
Q

What is JSON?

A

A format for representing data as a JavaScript object that is in string form

71
Q

What are serialization and deserialization?

A

Serialization is the process of turning data into a stream of bits, and deserialization is assembling these bits back into data

72
Q

Why are serialization and deserialization useful?

A

It is how data is transferred and retrieved

73
Q

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

A

JSON.stringify(obj)

74
Q

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

A

JSON.parse(str)

75
Q

How do you store data in localStorage?

A

localStorage.setItem(‘key’, ‘value’)

76
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(‘key’)

77
Q

What data type can localStorage save in the browser?

A

string

78
Q

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

A

Right before the page is closed

79
Q

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

A

A method definition uses the “function” keyword

80
Q

Describe method definition syntax (structure).

A

object.method = function (parameters) { };

81
Q

Describe method call syntax (structure).

A

object.method(arguments)

82
Q

What is the defining characteristic of Object-Oriented Programming?

A

Functionality is based around defining objects, which carry properties that can be modified using the object’s methods

83
Q

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

A

abstraction, encapsulation, inheritance, polymorphism

84
Q

What is “abstraction”?

A

Abstraction is creating a model of an entity and generally focusing on only the relevant properties of that entity

85
Q

What does API stand for?

A

Application Programming Interface

86
Q

What is the purpose of an API?

A

They are a prebuilt program that can be used by a programmer without needing to know the inner workings of the program

87
Q

What is “this” in JavaScript?

A

It is an implicit parameter in a function/method definition that refers to the immediate object the method belongs to; note that a function definition is actually a method definition of the window object

88
Q

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

A

“This” is automatically a parameter of every function/method that does not need to be stated

89
Q

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

A

call time

90
Q

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

A

For a method, it will be the object; for a function, it cannot be determined

91
Q

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

A

It is either the object immediately left of the method, or the window if no such object exists

92
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

Nothing, since the method is not being called

93
Q
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
A

“It’s-a-me, Mario!”

Since the greet method is being called on the object character, this.firstName will be character.firstName

94
Q
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
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!”

The variable hello is being assigned the greet method of the character object and not the rest of the object, resulting in “this” being the window, which does not have a firstName property which makes this.firstName undefined

95
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (or prototypal) inheritance

96
Q

What is a prototype in JavaScript?

A

A general version of an object that is used as a template for creating other objects using the Object.setPrototypeOf(obj, prototype) method

97
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

They were created using a prototype that included those methods

98
Q

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

A

In the __proto__ property of the object

99
Q

What does the new operator do?

A

It returns a new instance of an object using the specified constructor function and arguments

100
Q

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

A

ThatFunction.prototype

101
Q

What does the instanceof operator do?

A

Returns true if the prototype property of the second object appears anywhere in the prototype chain of the first object

102
Q

What is a “callback” function?

A

A function name used as an argument, allowing it to be used by the outer function

103
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, delay) function

104
Q

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

A

Using the setInterval(function, delay) function

105
Q

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

A

0; the callback function executes immediately when it comes up on the stack

106
Q

What do setTimeout() and setInterval() return?

A

They return an integer that represents the timer’s ID, which can be passed into the clearTimeout(ID)/clearInterval(ID) functions to cancel the respective timers

107
Q

What is AJAX?

A

Technology that allows asynchronous functionality in the browser using network requests

108
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

109
Q

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

A

Objects made from the XMLHttpRequest() constructor

110
Q

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

A

load

111
Q

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

A

They both contain the same prototype property in their respective prototype chains

112
Q

What is a code block? What are some examples of a code block?

A

A section of code within curly braces that is generally modular (e.g. function code block, for/while loop code block, conditional code block)

113
Q

What does block scope mean?

A

The section of code in which variables declared within the section will still persist

114
Q

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

A

The code block that they were declared in

115
Q

What is the difference between let and const?

A

const makes the variable read-only

116
Q

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

A

The method does not assign a new value (using the = operator) to the const variable

117
Q

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

A

Always try to use const when you can, and only use let when the variable needs to be mutable

118
Q

What is the syntax for writing a template literal?

A

The string is enclosed in backticks “`”

119
Q

What is “string interpolation”?

A

It is defining a string using a template literal with imbedded variables

120
Q

What is destructuring, conceptually?

A

A shorthand for getting properties from an object and assigning them to a new variable

121
Q

What is the syntax for Object destructuring?

A

const { property1, property2 } = object;

122
Q

What is the syntax for Array destructuring?

A

const [ valueAt0, valueAt1, …remainingValues ] = array;

123
Q

How can you tell the difference between destructuring and creating Object/Array literals?

A

The square brackets/curly braces are on the left side of the equal sign when you are destructuring

124
Q

What is the syntax for defining an arrow function?

A

(…parameters) => {code block};

125
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

JavaScript assumes the expression to the right of the arrow will be returned from the function

126
Q

How is the value of this determined within an arrow function?

A

Arrow functions do not bind their own this, and therefore adopt the value of this in the outer scope

127
Q

What are the 4 major concepts that sets JavaScript apart from many other languages?

A

this, prototypal inheritance, event loop, closure

128
Q

What is the JavaScript Event Loop?

A

It is the algorithm used for determining the task to be executed in the stack

129
Q

What is different between “blocking” and “non-blocking” with respect to how code is executed?

A

A blocking task goes directly on the call stack, while a non-blocking task joins a queue that feeds into the call stack when the stack is clear

130
Q

What are the three states a Promise can be in?

A

pending, fulfilled, rejected

131
Q

How do you handle the fulfillment of a Promise?

A

You pass a callback function into the prom.then() method

132
Q

How do you handle the rejection of a Promise?

A

You pass a second callback function into the prom.then() method, or pass a callback function into the prom.catch() method

133
Q

What is Array.prototype.filter useful for?

A

For creating a new array that contain only elements of an array that meet a certain criteria

134
Q

What is Array.prototype.map useful for?

A

When you want to transform every element in an array to make a new array

135
Q

What is Array.prototype.reduce useful for?

A

When you want to iterate over an array and return a value based on the accumulated values of the array

136
Q

What is “syntactic sugar”?

A

Additional syntax in a language that does not add to the language’s functionality, but allows for easier reading

137
Q

What is the typeof an ES6 class?

A

function

138
Q

Describe ES6 class syntax.

A
class ClassName {
constructor(...parameters) {
}

method(…parameters) {
}
}

139
Q

What is “refactoring”?

A

Changing code without changing it’s behavior

140
Q

How are ES Modules different from CommonJS modules?

A

It uses keywords import and export [default] instead of require() and module.exports

141
Q

What kind of modules can Webpack support?

A

ES (ECMA Script), Common JS, AMD (Async module definition), Assets, and WebAssembly modules

142
Q

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

A

a function

143
Q
What does wrap do?
const wrap = value => ( ) => value;
A

Takes an argument value and returns a function that returns value

144
Q

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

A

when it is defined

145
Q

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

A

When a function is declared within a function, JS creates a closure that holds the local variables at the time of declaration; this closure can be referenced when the declared function is called