JavaScript Flashcards

1
Q

What is the purpose of variables?

A

variables are used to store data/information to be used in scripts/functions

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

How do you declare a variable?

A

var variableName;

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

variableName = variableValue;
using the assignment operator

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 start with letter, $, or _. Can contain letters, numbers, $’s, or _’s.

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

name and Name are not the same 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

Strings can be used for names, usernames, messages, etc. that need to be displayed.

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

Numbers are used for mathematical functions, counting, positioning on a screen, etc.

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

Booleans display values of true or false.

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

The = operator is 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

By setting a new value to the variable name using the = 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

Null can be used as a placeholder value for a variable as it is still an object.
Undefined is when no value has been assigned to a variable yet.

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

Labels make it more clear the reason that something is being logged to the console, or what the value being logged represents.
Helps with 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, and 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

pushing two string values together into one string value

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

addition of numbers or concatenation of 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 values

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

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

A

adds a value on the right to the variable on the left, then assigns the new value to that same variable

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

What are objects used for?

A

Grouping together sets of variables and their values as well as methods, typically a set of traits describing a real-world object.

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

What are object properties?

A

variables stored within an object

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

Describe object literal notation.

A

variableName = {
property: value,
property: 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

using the delete operator
delete objectName.objectProperty

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
objectName.objectProperty = propertyValue;

bracket notation
objectName[‘objectProperty’] = propertyValue;

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

What are arrays used for?

A

storing lists of values under a variable name

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

Describe array literal notation.

A

arrayName = [item1, item2, item3]

arrayName[1]
item2

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 store data in order, allowing the use of the length property to pull values

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 length property is the number of values that are stored 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

subtract 1 from the length of an array
arrayName.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 block of code that can be called upon by name to run multiple times, taking input and providing output.

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 functionName(parameters, if any) {
lines of code being run;
more lines of code being run;
}

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

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

A function definition contains the block of code that will be running. A function call is just the name of the function and the arguments being taken, not any code being run.

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 in the function definition, and is a placeholder for what is needed in order for the function to run as designed.
An argument is in the function call, and are the values needed to fulfill what the parameter is asking for.

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

Why are function parameters useful?

A

They are placeholders for information that will be passed as arguments in the function. When a function is called, the arguments will be directed to where all the placeholders are located within the function code to successfully run the function.
Makes the function more flexible, being able to be used for more than one type of output.

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. It returns the value of a given variable to be used outside of the function block of code.
  2. It prevents any other code from being run in the function’s code block. (exits the function)
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 validate the expected results of a function

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

What is a method?

A

It is a function that 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

They are specific to an object and must be called in reference to that 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

pop method

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

floor method

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

random method

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

pop, shift, or splice methods

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 or unshift methods

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

use the split method with commas

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 change the original string, they create a new string. This is why the result must be stored in a new variable to be used

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

60 - 70 methods (look up string on MDN; left column)

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

It is only useful when values need to be assigned to a variable from a function, or when a function needs to be terminated.

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

40 - 50 methods

50
Q

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

51
Q

Give 6 examples of comparison operators.

A

> , <, >=, <=,

52
Q

What data type do comparison expressions evaluate to?

A

boolean

53
Q

What is the purpose of an if statement?

A

Allows for bifurcation in the code, so that different outcomes can be coded for based on the evaluation of an expression

54
Q

Is else required in order to use an if statement?

A

No

55
Q

Describe the syntax (structure) of an if statement.

A

if (expression) {
code block
} else {
code block
};

56
Q

What are the three logical operators?

A

&&, !, ||
and, not, and or

57
Q

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

A

using the logical operators in the expression block

58
Q

What is the purpose of a loop?

A

To repeat a block of code a specified (or unspecified) number of times

59
Q

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

A

To ensure that the loop is able to stop

60
Q

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

A

An iteration is akin to each “step” in the loop series. If the loop repeats 10 times, there are 10 iterations

61
Q

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

A

Right before each pass through the loop, including before the first pass through the loop

62
Q

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

A

One time before the loop begins

63
Q

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

A

Before each loop iteration, including the first iteration

64
Q

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

A

At the end of each loop iteration

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

increases a value by 1, and reassigns the value back to the variable

67
Q

How do you iterate through the keys of an object?

A

Using a for…in loop
for (var key in object) {
console.log(key); OR console.log(object[key]);
}

68
Q

What is the event.target?

A
69
Q

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

A

The elements and children are no longer displayed on the page, nor do they take up space

70
Q

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

A
71
Q

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

A

getAttribute()

72
Q

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

A

Anytime that code is expected to produce a return

73
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

add event listeners to each individual element

74
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

using an if else block to check each step of the code

75
Q

What is JSON?

A

JavaScript Object Notation
text-based format that represents data structures in JavaScript object syntax

76
Q

What are serialization and deserialization?

A

When data is taken from JavaScript object syntax into JSON text format, and the reverse of that process

77
Q

Why are serialization and deserialization useful?

A

It enables the transmission of data structures across networks

78
Q

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

A

JSON.stringify()

79
Q

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

A

JSON.parse()

80
Q

How do you store data in localStorage?

A

localStorage.setItem(keyName, keyValue)

81
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(keyName, keyValue)

82
Q

What data type can localStorage save in the browser?

A

JSON strings

83
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

84
Q

What is a method?

A

a function that is a property of an object

85
Q

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

A

method definition will have a code block, method call will just be the name followed by parentheses

86
Q

Describe method definition syntax (structure).

A

var object = {
methodName: function (parameters) {
code for function
code for function
}
}

87
Q

Describe method call syntax (structure).

A

object.method()

88
Q

How is a method different from any other function?

A

It is attached to an object

89
Q

What is the defining characteristic of Object-Oriented Programming?

A

objects can contain both data (properties) and behavior (methods)

90
Q

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

A

abstraction, encapsulation, inheritance, polymorphism

91
Q

What is “abstraction”?

A

simplifying concepts and interfaces for the programming

92
Q

What does API stand for?

A

application programming interface

93
Q

What is the purpose of an API?

A

allows people to easily interface with an application and its data
local storage is an example of an API

94
Q

What is this in JavaScript?

A

an implicit parameter that can be used to access properties and values in a JavaScript object

95
Q

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

A

It does not need to be declared as a parameter in the function definition to be accessible

96
Q

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

A

call time

97
Q

What does this refer to in the following code snippet?
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};

A

this refers to the character object

98
Q

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

A

‘It’s-a-me, Mario!’
this.firstName accesses the character object and return the value of the firstName property

99
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!
This occurs because the function in character.greet is being assigned to the variable hello, but the object is not being accessed by the function when called

100
Q

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

A

You can’t tell until the function/method is called

101
Q

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

A

It depends on the object to the left of the dot when called. If there is value to the left of the dot, the value is window

102
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based

103
Q

What is a prototype in JavaScript?

A

an object that contains properties and methods that can be used by other objects

104
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

If those methods exist on a prototype, they can be borrowed by the other elements

105
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 prototype chain

106
Q

What does the new operator do?

A

creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
For a clearer answer, reference the MDN Description section for the new operator

107
Q

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

A

prototype

108
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.
This is used to check whether an object was created as an instance of a specific constructor

109
Q

What is a “callback” function?

A

A function that is passed to another function

110
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() or setInterval()

111
Q

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

A

setInterval()

112
Q

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

A

Default time delay is 0, starting from when the page is loaded

113
Q

What do setTimeout() and setInterval() return?

A

an Interval ID

114
Q

What is AJAX?

A

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

115
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

116
Q

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

A

XMLHttpRequest

117
Q

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

A

load

118
Q

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

A

All JavaScript objects have prototypes with inherited methods

119
Q

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

A
120
Q

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

A
121
Q

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

A
122
Q

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

A