JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Variables 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

Use the var, let, or const 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

Follow the declaration with an “=“ and the value you’d like to assign

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

Start with letter, underscore, or dollarsign. Afterwards — numbers are okay. No keywords! No dashes or periods~!

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

What does it mean to say that variable names are “case-sensitive”

A

the two variables lovesDogs and lovesdogs are different due to the D / d.

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

Stores text-data, good for manipulation.

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

Stores number-data, good for mathematics

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

Stores true/false data, good for logic

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

Assignment operator — assigns right value to the left operand

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

Use the = operator with the variable on the left, and the new value on the right.

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

What is the difference between null and undefined?

A

Null is an intentionally empty value, undefined means no value was 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 logging values to the console?

A

When you add multiple console logs it gets confusing pretty quickly what you logging.

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

Give five examples of JavaScript primitives.

A

Number, string, Boolean, null, undefined

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

What is a JavaScript primitive?

A

A data that is not an object, and hence has no methods or properties.

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

What data type is retuned by an arithmetic operation?

A

Number is returned

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

What is string concatenation?

A

Joining together two strings and returning a new string

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

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

A

Adds two operands (not necessarily nums!)

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

What data type is returned by comparing two values (<, >, ===, etc.)?

A

Boolean (t / f)

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

What do the += “plus-equals’ operator do?

A

Shorthand — adds the value of right side to the left side variable and returns the result

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

How are reference data types and primitive data types stored differently?

A

Reference data types only store the memory pointer to that specific data

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

The two reference data types are…

A

Objects and arrays

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

What are objects used for?

A

To group together variables and functions in order to model something from the real world

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

What are object properties?

A

The variables of the object are its properties (its functions are known methods)

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

Describe object literal notation.

A

Declare variable and assign it the value of empty curly braces

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

How do you remove a property from an object

A

Use the delete keyword followed by the property and value

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

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

A

Dot & bracket notation

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

Dot notation vs bracket notation differences?

A

Dot notation requires a legal variable name —> bracket notation can also be used with variables

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

What are arrays used for?

A

Storing related data of unknown length — sort of like lists

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

Describe array literal notation.

A

var array = [data seperated by commas

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

How are arrays different from plain objects?

A

array keys are index numbers, object keys are assigned by us

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

What number represents the first index of an array?

A

Arrays are 0-indexed

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

What is the length property of an array?

A

It holds a number with the amount of items in the array

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

How do you calculate the last index of an array?

A

Use array.length - 1

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

What is a function in JavaScript?

A

A set of statements that performs a task — often with an input and output.

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

Describe the parts of a function definition.

A

Function keyword, function name (opt), (paramteter list), {code body, return(opt)}

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

Describe the parts of a function call.

A

Function name, (arguments)

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

How are function calls and function definitions different?

A

Definitions are stored in memory, calls look in memory and run the stored function

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

What is the difference between a parameter and an argument?

A

Parameters are for definitions, no concrete value —> arguments are for calling, has value

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

Why are function parameters useful?

A

They allow us to take advantage of a functions functionality by passing in specific arguments

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

What two effects does a return statement have on the behavior of a function?

A
  1. kicks you out of that execution context
  2. Specifies a value to be returned to the function caller
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Why do we log things to the console?

A

It helps with debugging —> we can see what is stored in memory at that point

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

What is a method?

A

A property on objects with a function stored as the value

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

How is a method different from any other function?

A

Methods are linked directly to their related object —> otherwise they’re the exact same

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

How do you remove the last element from an array?

A

The pop method (no args)

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

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

A

The floor method (number to execute on)

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

How do you generate a random number?

A

Use the random method to get a num between 0 -> 1 (no args)

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

How do you delete an element from an array?

A

The splice method (takes starting index and number of elements to remove)

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

How do you append an element to an array?

A

The push method (element to push)

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

How do you break a string up into an array?

A

The split method (takes the character to split with)

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

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

A

Nope —> console.log(original string) to check for changes!

51
Q

Roughly how many string methods are there according to MDN?

A

~50!

52
Q

Roughly how many array methods are there?

A

~80!

53
Q

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

A

Nope! Some methods alter the original array (like pop) & return undefined

54
Q

What three-letter acronym should you always include when searching Google for answers?

A

MDN!

55
Q

Give 6 examples of comparison operators.

A

<, >, <=, >=, ==, !=, !==, ===

56
Q

What data type do comparison expressions evaluate to?

A

Boolean t/f

57
Q

What is the purpose of an if statement?

A

To evaluate a condition and run code specific to that condition

58
Q

Is “else” required in order to use an if statement?

A

Nope! (But it’s good practice if you’re trying to catch edge cases)

59
Q

Describe the syntax of an if statement.

A

If keyword (condition) {code block to run if condition is satisified}

60
Q

What are the three logical operators?

A

&& (and) || (or) ! (Not)

61
Q

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

A

Write the conditions in the same parenthesis, seperated by the desired logical operator.

62
Q

What is the purpose of a loop?

A

To repeat code!

63
Q

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

A

It tells the loop when to stop!

64
Q

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

A

Repeating some code over some data

65
Q

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

A
  1. Before the loop starts
  2. After each iteration
66
Q

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

A

The first time the loop runs

67
Q

When does the condition of a for loop get evaluated?

A
  1. After the initialization statement
  2. After each final expression
68
Q

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

A

After each iteration of the loop

69
Q

Besides the return statement, what keyword exits the loop before it evaluates to false?

A

Break statement will break the loop, but not the function

70
Q

What does the ++ increment operator do?

A
  1. It increases the value of the affected variable by 1
  2. It substitutes first and THEN increments
71
Q

How do you iterate through the keys of an object?

A

Use a for / in loop!

72
Q

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

A

The focus event

73
Q

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

A

The blur event

74
Q

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

A

The input event

75
Q

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

A

The submit event

76
Q

What does the event.preventDefault() method do?

A

It prevents the page from reloading after submitting a form (default behavior)

77
Q

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

A

The form.elements property —> make sure you check legal variable names for {.notation}

78
Q

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

A

Form.elements.(element-name).value

79
Q

What is one risk of writing a lot of code without checking if it works?

A

When you inevitably run into a bug, you have no idea what part of the code it came from

80
Q

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

A

You can use it alongside console.logs to make sure your code does what you think it does

81
Q

Pretty much all of the time you create a form, the first line will be…

A

event.preventDefault( );

82
Q

What is the event.target?

A

The element that dispatched the event (the one the user interacted with)

83
Q

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

A

It hides all content within that element from the user.

84
Q

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

A

Takes CSS selectors as the argument, returns true if the element matches the selector(s), otherwise returns false

85
Q

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

A

Use the getAttribute( ) method with the HTML attribute you are looking for.

86
Q

If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JS code be written instead?

A

You would have to create all those elements, give them the appropriate classes, append them as children, and give each child the appropriate event listener to hide/show on click.

87
Q

If you didn’t use a loop to conditionally show or hide the views, how would your JS code be written instead?

A

You would have to add event listeners for each view item, and when that view item is clicked bring it into view, while hiding the other view items.

88
Q

What is JSON?

A

JavaScript Object Notation —> text based format for represented structured data in objects

89
Q

What are serialization and deserialization?

A

S: process of turning an object into a stream of bytes so you can send it / store it.
D: turnings stream of sent / stored information into an object in memory

90
Q

Why are serialization and deserialization useful?

A

Whenever we want to send or store JS objects, we need a way to do it!

91
Q

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

A

JSON.stringify( ); parameters: value, replacer(opt.), space(opt.)

92
Q

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

A

JSON.parse( ); parameters: text, reviver(opt.)

93
Q

How do you store data in localStorage?

A

Use the setItem method and pass in the key and value of what you are storing (JSON value!)

94
Q

How do you retrieve data from localStorage?

A

Use the getItem method and pass in the key of the JSON item you are pulling

95
Q

What data type can localStorage save in the browser?

A

JSON

96
Q

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

A

It happens before the page gets rid of all the data — before you leave the page, typically.

97
Q

What is ‘this’ in JavaScript?

A

It’s an implicit parameter defined when the function is called

98
Q

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

A

It’s available in the code block even though it was never explicitly declared.

99
Q

When is the value of ‘this’ determined in a function?

A

When the function is 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 — this is not determined until that function is called. Look for where it’s called

101
Q

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

A

Look for the object to the left of the dot. If there’s no value there, ‘this’ is the global window object

102
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypical inheritance

103
Q

What is a prototype in JavaScript?

A

A mechanism that JS objects use to inherit features from one another

104
Q

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

A

Through prototypical inheritance! The factory that makes strings attaches a prototype chain to strings that allows them to access the methods we are familiar with.

105
Q

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

A

The prototype chain!

106
Q

What does the new operator do?

A

The new operator creates an instance of an object. The type of instance is determined by the constructor following the new keyword.

107
Q

Specifically, the 4 things the new operator does are…

A
  1. Creates a blank, plain JS object.
  2. Points newInstance’s [[prototype]] to the constructor function’s prototype property.
  3. It binds the ‘this’ keyword to that new object.
  4. If the constructor returns a non-primitive, the return value becomes the result of the entire new expression (but we never return anything from these constructors!)
108
Q

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

A

The prototype property allows shared behavior between instances created with new.

109
Q

What does the instanceof operator do?

A

The instance of operator returns a bool (t / f) whether the object to the right is an instanceof the left.

110
Q

What is a “callback” function?

A

A function that passed into another function as an argument.

111
Q

Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JS function until some point in the future?

A

Use setTimeout(cb fx, time(Ms))

112
Q

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

A

Use setInterval (cb fx, time(Ms) —> stop interval using clearInterval

113
Q

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

A

0 —> the code will execute immediately (or more accurately, the next event cycle)

114
Q

What do setTimeout() and setInterval() return?

A

They both return an ID —> can be passed to clearTimeout()/Interval() to cancel the timeout / interval.

115
Q

What is array.prototype.filter useful for?

A

It’s good when you need to grab elements from an array that satisfy a certain condition

116
Q

What is array.prototype.map useful for?

A

It’s good for when you want to do something to every element of the array

117
Q

What is array.prototype.reduce useful for?

A

It’s good for when you want to reduce an array down to one value.

118
Q

If your goal is to return an object from reduce, don’t forget to…

A

Start the recursion with a new empty object to prevent reference errors

119
Q

What is “synatic sugar”

A

Syntatic sugar makes the code easier to read and understand

120
Q

What is the type of an ES6 class?

A

It’s a function!

121
Q

Describe ES6 class syntax?

A

Class keyword, name of class, optionally a constructor, optionally a method

122
Q

What is refactoring?

A

Changing code structure without affecting it’s functionality.

123
Q

Why is prototyping beneficial?

A

If we define a function on the constructor, each instance of that object gets a copy of that function (which is super overkill —> takes up a lot of memory). Defining it on the prototype means that all instances point to that same function (which saves memory)