JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data

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 ‘var’ 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

Using the “=” sign

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

Must begin with a letter, ($), or (_)

Can’t start with numbers or use (-) or (.)

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

value and Value are 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 store a string 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

To store a numeric value like amount

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 determine if something should be done; should code run

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

To assign, store 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

You re-assign a variable; don’t need to use the var keyword

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 when variable is not assigned a value, null is purposeful emptiness;
Null has 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

Clarity for what is being logged

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

What are objects used for?

A

Group together a set of variables and functions to create a model of something.

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

What are object properties?

A

a variable that is part of an object

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

Describe object literal notation.

A
var object = {
  key: 'value',
  key: 40
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you remove a property from an object?

A

Using the delete keyword;

delete object.propertyName

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

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

A

Dot notation: object.popertyName;

Bracket notation: object[‘propertyName’];

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

Give five examples of JavaScript primitives.

A

strings, numbers, booleans, undefined, null

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

What data type is returned by an arithmetic operation?

A

numbers

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

What is string concatenation?

A

Combine 2 strings into a new string

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

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

A

Add numbers or concatenate strings

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

What data type is returned by comparing two values (greater than, less than, ===, etc.)

A

Booleans

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

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

A

Combines the variable and the value and assigns the new value to the variable

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

What are arrays used for?

A

To store a list of values

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

Describe array literal notation.

A

var array = [‘value1’, ‘value2’, ‘value3’];

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 uses index numbers and objects use property names.

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 number of items in the array

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

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

Functions allow you to package up code for use later in your 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

Function definitions are made of:

  1. the function keyword
  2. an optional name
  3. zero or more parameters
  4. a code block
  5. an optional return statement
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 () parentheses.

example(arg1, arg2, arg3);

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

Definition will have function keyword and { }

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 place holder of unknown value until function is called and argument is passed. When a function is called, the parameters in its definition take on the values of the arguments that were passed.

when we define a function, we declare parameters and that 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

Way to give data to functions, way to provide data to get different results of 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. A return statement causes the function to produce a value.
  2. A return statement also exits the function; no code after the return statement is executed.
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

For code to communicate with us and see if feature is working.
So we know what is being returned from a function or method.

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 attached to an object; Not a free floating function; potential to use data attached to 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

The pop( ) method removes the last element from an array and returns that element. This method changes the length of the array.

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

The Math.floor( ) function returns the largest integer less than or equal to a given 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

The Math.random( ) function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

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( ) method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

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() method adds one or more elements to the end of an array and returns the new length of the array.

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( ) method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.

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. You can console.log or check MDN.

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, sometimes the return isn’t needed EX: push method returns the length but I didn’t need it for the exercise.

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

Give 6 examples of comparison operators.

A
=== strict equal to
!== strict not equal to 
> greater than
< less than
>= greater than or equal to
<= less than or equal to
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What data type do comparison expressions evaluate to?

A

booleans

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

What is the purpose of an ‘if’ statement?

A

the ‘if’ statment evaluates (or checks) a condition. if condition evaluates to ‘true’, any statements in the subsequent code block are executed.

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

Describe the syntax (structure) of an ‘if’ statement.

A

if (condition) {
code block
}

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

What are the three logical operators?

A

&& logical and
|| logical or
! logical not

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

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

A

Use logical operator && or ||

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

What is the purpose of a loop?

A

To repeat an action / code.

57
Q

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

A

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed.

Determines when loop stops.

58
Q

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

A

Number of times the code loops.

59
Q

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

A

Before each pass through the loop/iteration.

60
Q

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

A

Once before the loop begins / before anything else in the loop.

61
Q

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

A

Before each loop iteration.

62
Q

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

A

End of each loop iteration / after the code block is run.

63
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’ statement

64
Q

What does the ++ increment operator do?

A

increments (adds one to) its operand and returns a value

65
Q

How do you iterate through the keys of an object?

A

for…in loop

66
Q

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

A

focus

67
Q

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

A

blur

68
Q

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

A

input

69
Q

What event is fired when a user clicks the “submit” button within a ‘form’ element?

A

submit

70
Q

What does the event.preventDefault() method do?

A

Default action should not be taken (checkbox won’t check); the action doesn’t happen; prevent default behavior that would have happened from that event

The Event interface’s preventDefault() method 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. The event continues to propagate as usual, unless one of its event listeners calls stopPropagation() or stopImmediatePropagation(), either of which terminates propagation at once.

71
Q

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

A

Refreshes page and adds input data to URL

72
Q

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

A

element property

73
Q

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

A

value property

74
Q

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

A

Not knowing which step is failing or where the error is occurring.

75
Q

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

A

To test and check the code.

76
Q

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

A

Turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist; document flow). All descendant elements also have their display turned off.

77
Q

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

A

Argument: string of ‘css selector’

Return: a boolean value whether argument matches element

78
Q

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

A

getAttribute() method

let attribute = element.getAttribute(attributeName);

79
Q

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

A

Every step you write new code to do something.

Every variable declaration, for loops, if statements.

80
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 have to write an event listener for each tab/view element and query them.

81
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 have to write a separate conditional for each view element

82
Q

What is JSON?

A

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

Exists as a string

83
Q

What are serialization and deserialization?

A

Converting a string to a native object is called deserialization, while converting a native object to a string so it can be transmitted across the network is called serialization.

84
Q

Why are serialization and deserialization useful?

A

transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

so we can store data; disc persistence

85
Q

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

A

JSON.stringify() method

86
Q

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

A

JSON.parse() method

87
Q

How to you store data in ‘localStorage’?

A

.setItem() method

localStorage.setItem(‘key-name’, ‘key-value’);

88
Q

How to you retrieve data from ‘localStorage’?

A

.getItem() method

localStorage.getItem(‘key-name’);

89
Q

What data type can ‘localStorage’ save in the browser?

A

string

90
Q

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

A

When the window, the document and its resources are about to be unloaded.

Before refresh or close window/tab

91
Q

What is a method?

A

Function attached to a property of an object

92
Q

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

A

object.method() for called; function definition being assigned to a property in an object.

93
Q

Describe method definition syntax (structure).

A
var object = {
  propertyName: function (parameter) {
    code-block;
  }
};
94
Q

Describe method call syntax (structure).

A

object.methodName( );

95
Q

How is a method different from any other function?

A

Methods are attached to an object as a property.

Function attached to an object; accessing data stored in that object

96
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

Grouping data with behavior; pairs data with behavior, shared same space

97
Q

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

A

Abstraction, Encapsulation, Inheritance, Polymorphism

98
Q

What is “abstraction”?

A

Being able to work with (possibly) complex things in simple ways.

Taking a complex process for simple use?

99
Q

What does API stand for?

A

Application programming interface

100
Q

What is the purpose of an API?

A

To give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

Taking a complex program and simplifying how to interact with it.

101
Q

What is ‘this’ in JavaScript?

A

An implicit parameter of all JavaScript functions.

inside of ‘this’ is the current object; object left of the dot; object working inside of currently

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

103
Q

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

A

When the function is called.

104
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

Character variable/object

105
Q

Given the above ‘character’ object, what is the result of the following code snippet? Why?

“character.greet();”

A

“It’s-a-me, Mario!” Mario is the value of the firstName property of the character object.

‘this’ applies to left of ‘.’ which is the character object;

106
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!”

function definition is not attached to object; free floating function; there is nothing to the left of the dot of hello() because there is no dot; not attached to an object; object becomes window, but there is not firstName property of the window object.

107
Q

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

A

You cannot determine until it gets called

108
Q

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

A

Object left of dot, if no object it will be the global window

109
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (or prototypal)

Behaviour reuse (known as inheritance) is performed via a process of reusing existing objects that serve as prototypes.

To move properties and methods that we’d like to reuse into a “prototype” object and then tell other objects to simply delegate (verb) to that “prototype” object when they aren’t able to perform the required tasks themselves.

110
Q

What is a prototype in JavaScript?

A

A JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be used by other objects.

111
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

Methods are defined on a “prototype” object and arrays/strings/numbers simply borrow those methods when they’re needed. ??

112
Q

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

A

__proto__ object

113
Q

What does the ‘new’ operator do?

A
  1. Creates a blank, plain JavaScript object.
  2. Adds a property to the new object (__proto__) that links to the constructor function’s prototype object.
  3. Binds the newly created object instance as the ‘this’ context (i.e. all references to ‘this’ in the constructor function now refer to the object created in the first step).
  4. Returns ‘this’ if the function doesn’t return an object.
114
Q

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

A

prototype property

115
Q

What does the ‘instanceof’ operator do?

A

Tests to see if the ‘prototype’ property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

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

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() method sets a timer which executes a function or specified piece of code once the timer expires.

var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, …]);

118
Q

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

A

setInterval() method repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

clearInterval() method cancels a timed, repeating action which was previously established by a call to setInterval(). (Stops repetition)

var intervalID = scope.setInterval(func, [delay, arg1, arg2, …]);

scope.clearInterval(intervalID)

119
Q

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

A

0 ms

120
Q

What do ‘setTimeout()’ and ‘setInterval()’ return?

A

A positive numeric, non-zero value which identifies the timer created

121
Q

What is AJAX?

A

A technique for loading data into part of a page without having to refresh the entire page. The data is often sent in a format called JavaScript Object Notation (or JSON).

122
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML.

123
Q

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

A

XMLHttpRequest (XHR)

124
Q

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

A

load

125
Q

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

A

Same prototype property?? Prototype inheritance??

126
Q

What is ‘Array.prototype.filter’ useful for?

A

Creating a new array with all elements that pass a test implemented by the provided function.

127
Q

What is ‘Array.prototype.map’ useful for?

A

Creating a new array containing the transformed elements of another.

128
Q

What is ‘Array.prototype.reduce’ useful for?

A

Combining the elements of an array into a single value.

129
Q

What does ‘fetch()’ return?

A

a Promise object

A Promise that resolves to a Response object.

130
Q

What is the default request method used by ‘fetch()’?

A

GET

131
Q

How do you specify the request method (GET, POST, etc.) when calling ‘fetch’?

A

Passing the ‘init’ object with property ‘method’

132
Q

When does React call a component’s ‘componentDidMount’ method?

A

Immediately after component is mounted (inserted into the tree).

133
Q

Name three React.Component lifecycle methods.

A

Mounting: constructor(), render(), componentDidMount()
Updating: render(), componentDidUpdate()
Unmounting: componentWillUnmount()

134
Q

How do you pass data to a child component?

A

As a prop

135
Q

What must the return value of ‘myFunction’ be if the following expression is possible?

myFunction()();

A

The return of the function returned from myFunction??

136
Q

What does this code do?

const wrap = value => () => value;

A

wrap function returns a function performing the code stated in the ‘value’ parameter of wrap??

137
Q

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

A

When it is defined??

138
Q

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

A

lexical scoping??

lexical environment??

139
Q

What is a closure?

A

A closure is the combination of a function and the lexical environment within which that function was declared.

function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);

console. log(add5(2)); // 7
console. log(add10(2)); // 12

add5 and add10 are both closures. They share the same function body definition, but store different lexical environments. In add5’s lexical environment, x is 5, while in the lexical environment for add10, x is 10.