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
How do you remove a property from an object
Use the delete keyword followed by the property and value
26
What are two ways to get or update the value of a property?
Dot & bracket notation
27
Dot notation vs bracket notation differences?
Dot notation requires a legal variable name —> bracket notation can also be used with variables
28
What are arrays used for?
Storing related data of unknown length — sort of like lists
29
Describe array literal notation.
var array = [data seperated by commas
30
How are arrays different from plain objects?
array keys are index numbers, object keys are assigned by us
31
What number represents the first index of an array?
Arrays are 0-indexed
32
What is the length property of an array?
It holds a number with the amount of items in the array
33
How do you calculate the last index of an array?
Use array.length - 1
34
What is a function in JavaScript?
A set of statements that performs a task — often with an input and output.
35
Describe the parts of a function definition.
Function keyword, function name (opt), (paramteter list), {code body, return(opt)}
36
Describe the parts of a function call.
Function name, (arguments)
37
How are function calls and function definitions different?
Definitions are stored in memory, calls look in memory and run the stored function
38
What is the difference between a parameter and an argument?
Parameters are for definitions, no concrete value —> arguments are for calling, has value
39
Why are function parameters useful?
They allow us to take advantage of a functions functionality by passing in specific arguments
40
What two effects does a return statement have on the behavior of a function?
1. kicks you out of that execution context 2. Specifies a value to be returned to the function caller
41
Why do we log things to the console?
It helps with debugging —> we can see what is stored in memory at that point
42
What is a method?
A property on objects with a function stored as the value
43
How is a method different from any other function?
Methods are linked directly to their related object —> otherwise they’re the exact same
44
How do you remove the last element from an array?
The pop method (no args)
45
How do you round a number down to the nearest integer?
The floor method (number to execute on)
46
How do you generate a random number?
Use the random method to get a num between 0 -> 1 (no args)
47
How do you delete an element from an array?
The splice method (takes starting index and number of elements to remove)
48
How do you append an element to an array?
The push method (element to push)
49
How do you break a string up into an array?
The split method (takes the character to split with)
50
Do string methods change the original string? How would you check if you weren’t sure?
Nope —> console.log(original string) to check for changes!
51
Roughly how many string methods are there according to MDN?
~50!
52
Roughly how many array methods are there?
~80!
53
Is the return value of a function or method useful in every situation?
Nope! Some methods alter the original array (like pop) & return undefined
54
What three-letter acronym should you always include when searching Google for answers?
MDN!
55
Give 6 examples of comparison operators.
<, >, <=, >=, ==, !=, !==, ===
56
What data type do comparison expressions evaluate to?
Boolean t/f
57
What is the purpose of an if statement?
To evaluate a condition and run code specific to that condition
58
Is “else” required in order to use an if statement?
Nope! (But it’s good practice if you’re trying to catch edge cases)
59
Describe the syntax of an if statement.
If keyword (condition) {code block to run if condition is satisified}
60
What are the three logical operators?
&& (and) || (or) ! (Not)
61
How do you compare two different expressions in the same condition?
Write the conditions in the same parenthesis, seperated by the desired logical operator.
62
What is the purpose of a loop?
To repeat code!
63
What is the purpose of a condition expression in a loop?
It tells the loop when to stop!
64
What does “iteration” mean in the context of loops?
Repeating some code over some data
65
When does the condition expression of a while loop get evaluated?
1. Before the loop starts 2. After each iteration
66
When does the initialization expression of a for loop get evaluated?
The first time the loop runs
67
When does the condition of a for loop get evaluated?
1. After the initialization statement 2. After each final expression
68
When does the final expression of a for loop get evaluated?
After each iteration of the loop
69
Besides the return statement, what keyword exits the loop before it evaluates to false?
Break statement will break the loop, but **not** the function
70
What does the ++ increment operator do?
1. It increases the value of the affected variable by 1 2. It substitutes first and THEN increments
71
How do you iterate through the keys of an object?
Use a for / in loop!
72
What event is fired when a user places their cursor in a form control?
The focus event
73
What event is fired when a user’s cursor leaves a form control?
The blur event
74
What event is fired as a user changes the value of a form control?
The input event
75
What event is fired when a user clicks the “submit” button in a form?
The submit event
76
What does the event.preventDefault() method do?
It prevents the page from reloading after submitting a form (default behavior)
77
What property of a form element object contains all of the form controls?
The form.elements property —> make sure you check legal variable names for {.notation}
78
What property of a form control object gets and sets its value?
Form.elements.(element-name).value
79
What is one risk of writing a lot of code without checking if it works?
When you inevitably run into a bug, you have no idea what part of the code it came from
80
What is an advantage of having your console open when writing a JavaScript program?
You can use it alongside console.logs to make sure your code does what you think it does
81
Pretty much all of the time you create a form, the first line will be…
event.preventDefault( );
82
What is the event.target?
The element that dispatched the event (the one the user interacted with)
83
What is the effect of setting an element to display: none?
It hides all content within that element from the user.
84
What does the element.matches( ) method take as an argument and what does it return?
Takes CSS selectors as the argument, returns true if the element matches the selector(s), otherwise returns false
85
How can you retrieve the value of an element’s attribute?
Use the getAttribute( ) method with the HTML attribute you are looking for.
86
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?
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
If you didn’t use a loop to conditionally show or hide the views, how would your JS code be written instead?
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
What is JSON?
JavaScript Object Notation —> text based format for represented structured data in objects
89
What are serialization and deserialization?
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
Why are serialization and deserialization useful?
Whenever we want to send or store JS objects, we need a way to do it!
91
How do you serialize a data structure into a JSON string using JS?
JSON.stringify( ); parameters: value, replacer(opt.), space(opt.)
92
How do you deserialize a JSON string into a data structure using JS?
JSON.parse( ); parameters: text, reviver(opt.)
93
How do you store data in localStorage?
Use the setItem method and pass in the key and value of what you are storing (JSON value!)
94
How do you retrieve data from localStorage?
Use the getItem method and pass in the key of the JSON item you are pulling
95
What data type can localStorage save in the browser?
JSON
96
When does the ‘beforeunload’ event fire on the window object?
It happens before the page gets rid of all the data — before you leave the page, typically.
97
What is ‘this’ in JavaScript?
It’s an implicit parameter defined when the function is called
98
What does it mean to say that this is an “implicit” parameter?
It’s available in the code block even though it was never explicitly declared.
99
When is the value of ‘this’ determined in a function?
When the function is called.
100
How can you tell what the value of this will be for a particular function or method definition?
You can’t — this is not determined until that function is called. Look for where it’s called
101
How can you tell what the value of this is for a particular function or method call?
Look for the object to the left of the dot. If there’s no value there, ‘this’ is the global window object
102
What kind of inheritance does the JavaScript programming language use?
Prototypical inheritance
103
What is a prototype in JavaScript?
A mechanism that JS objects use to inherit features from one another
104
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t exist on strings, arrays, and numbers?
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
If an object does not have it’s own property of method by a given key, where does JavaScript look for it?
The prototype chain!
106
What does the new operator do?
The new operator creates an instance of an object. The type of instance is determined by the constructor following the new keyword.
107
Specifically, the 4 things the new operator does are…
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
What property of JavaScript functions can store shared behavior for instances created with new?
The prototype property allows shared behavior between instances created with new.
109
What does the instanceof operator do?
The instance of operator returns a bool (t / f) whether the object to the right is an instanceof the left.
110
What is a “callback” function?
A function that passed into another function as an argument.
111
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?
Use setTimeout(cb fx, time(Ms))
112
How can you set up a function to be called repeatedly without using a loop?
Use setInterval (cb fx, time(Ms) —> stop interval using clearInterval
113
What is the default time delay if you omit the delay parameter from setTimeout( ) or setInterval( )
0 —> the code will execute immediately (or more accurately, the next event cycle)
114
What do setTimeout() and setInterval() return?
They both return an ID —> can be passed to clearTimeout()/Interval() to cancel the timeout / interval.
115
What is array.prototype.filter useful for?
It’s good when you need to grab elements from an array that satisfy a certain condition
116
What is array.prototype.map useful for?
It’s good for when you want to do something to every element of the array
117
What is array.prototype.reduce useful for?
It’s good for when you want to reduce an array down to one value.
118
If your goal is to return an object from reduce, don’t forget to…
Start the recursion with a new empty object to prevent reference errors
119
What is “synatic sugar”
Syntatic sugar makes the code easier to read and understand
120
What is the type of an ES6 class?
It’s a function!
121
Describe ES6 class syntax?
Class keyword, name of class, optionally a constructor, optionally a method
122
What is refactoring?
Changing code structure without affecting it’s functionality.
123
Why is prototyping beneficial?
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)