JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Data storage for scripts to call back to

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

How do you declare a variable?

A

They are declared with
- var
- let
- const

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

Variables are initialized by including the assignment operator ‘=’
example: var example = true

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
  • letters
  • numbers (CANNOT be the first character of a variable name)
  • underscores(_)
  • dollar signs
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

Variables names must follow the same casing when used later in a script, so the variables “fullName” and “FullName” would be detected as two different variables

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 text (a sequence 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 piece of numerical data (for math/calculations)

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 store a state of something is or is not

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

This is the assignment operator, it initializes variables and can re-assign their values.

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

Re-assign it with the assignment 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 is a value explicitly assigned to not exist
Undefined is a default value set to variables that have not been assigned

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

Give five examples of JavaScript primitives.

A
  • Number
  • Boolean
  • String
  • Undefined
  • Null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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
14
Q

What is string concatenation?

A

The adding of a string to the end of another string
The result is always a string.

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

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

A

It tells values to combine by either concatenating or adding

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

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

A

Boolean (true/false)

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

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

A

Addition assignment operator: adds the value of the right operand to a variable and assigns the result to the variable. Can be used to add or concatenate.

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

What are objects used for?

A

They are used to group related variables and functions

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

What are object properties?

A

Related variables/functions stored in an object become a property/method of the object

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

Describe object literal notation.

A

list out the properties and methods within curly braces and assign it to a variable

{
     hasProperty: true,
     name: 'example',
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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

A

Dot notation and bracket notation
exampleObject.name' -dot notation
exampleObject['name'] -bracket notation

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

What are arrays used for?

A

They are used to store lists of data where the order is important or unimportant
Note: Unlike objects the key that the variables are paired to are numbers as they are indexed.

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

Describe array literal notation.

A

A list contained in two square-brackets ([ ]) with each value separated with commas

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

How are arrays different from “plain” objects?

A

The key that the variables are paired to are numbers and not properties. They are accessed with bracket-notation unless using a method.
(ex.)
console.log(exampleArray[0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What number represents the first index of an array?
0
26
What is the `length` property of an array?
A property that returns the length of the array/object
27
How do you calculate the last index of an array?
Subtract the array length by 1 This is due to the array indexing the variables stored within using zero-based counting *(ex.)* `var lastIndex = exampleArray.length - 1;`
28
What is a function in JavaScript?
A special object that can run a code block as many times as it is called
29
Describe the parts of a function **definition**
includes the keyword function, a name for the function (optional), parameters separated by commas (if any), the code block, and the return statement (optional) *(ex.)* ``` function exampleFunction(word) { var completedPhrase = 'Hello, ' + word; return completedPhrase; } ```
30
Describe the parts of a function **call**
includes the name for the function followed by any arguments in parentheses *(ex.) `exampleFunction(arg1, arg2)`
31
What are the differences between a function **call** and a function **definition**
A function call only includes the function name and any arguments *instead of parameters* A function definition includes the keyword function *and* the name, as well as parameters *instead of arguments*, and a code block to be run
32
What is the difference between a **parameter** and an **argument**
A parameter is used to define a function, while an argument is a real number passed through the function.
33
6 examples of comparison operators
- < *less than* - > *greater than* - <= *less than or equal to* - >= *greater than or equal to* - === *strictly equal to* - !== *strictly not equal to*
34
What data type do comparison expressions evaluate to?
Boolean
35
What is the purpose of an `if` statement?
To allow a specific condition to determine whether to run a code block or not
36
Is `else` required in order to use an `if` statement?
No, else is optional
37
Describe the syntax (structure) of an `if` statement
``` if (condition) { } ```
38
What are the three logical operators?
- Or (||) - And (&&) - Not (!)
39
How do you compare two different expressions in the same condition?
Using a logical operator
40
Why do we log things to the console?
Debugging purposes, if we can see what value a variable is at different sections of the code, we can figure out where things went wrong.
41
What is a method?
A function that is stored as a property in an object
42
How is a method different from any other function?
It is associated with the object it is a method of
43
How do you remove the last element from an array?
the .pop() method, which is implicit to arrays
44
How do you round a number down to the nearest integer?
`Math.floor()` The floor method of the Math object will take a value passed as an argument and round it down. *Note: optional parameters allow for changing the exponent and type of adjustment*
45
How do you generate a random number?
`Math.random()` The random method of the Math object generates a pseudo-random float value between 0 and 1.
46
How do you delete an element from an array?
``` arrayName.pop(); arrayName.shift(); arrayName.splice(); ``` Can be used to delete an element from an array
47
How do you append an element to an array?
``` arrayName.push(); ```
48
How do you break a string up into an array?
``` .split() - will split a string into an array based on the arguments passed through ```
49
Do string methods change the original string? How would you check if you weren't sure?
No they do not. Strings are **immutable**. If one wanted to check one could log the string before and after using a method to see if it changed after the method was applied.
50
Is the return value of a function/value useful in every situation?
No, if what we need is the original data processed and not whatever we are calculating/extracting
51
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
52
What is the purpose of a loop?
To re-run a code block. One use: To run code block for each value in an array
53
What is the purpose of a **condition** expression in a loop?
To specify when a loop should stop
54
What does "iteration" mean in the context of loops?
Each pass of the code block "This is the nth time the loop has run"
55
*When* does the **condition** expression of a `while` loop get evaluated?
It runs before each code block iteration
56
*When* does the **initialization** expression of a `for` loop get evaluated?
Before anything and only before the first iteration
57
*When* does the condition expression of a `for` loop get evaluated?
After the initialization and before each iteration of the code block
58
When does the final expression of a `for` loop get evaluated?
After each iteration
59
Besides a `return` statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
`break;`
60
What does the ++ increment operator do?
Increases value by 1
61
How do you iterate through the keys of an object?
A for...in loop
62
What is JSON?
JavaScript Object Notation(JSON) is a string-based format for representing data/data structures
63
What are serialization and deserialization?
Serialization is the conversion of a data structure into a string of bytes for the sake of storage or transporting it between systems. Deserialization is the conversion of those strings of bytes back into a copy of the original data structure in the same state.
64
Why are serialization and deserialization useful?
It allows data structures to be sent between differing systems and formats while still maintaining what that data structure is actually representing.
65
How do you serialize a data structure into a JSON string using JavaScript?
`JSON.stringify(exampleDataStructure)`
66
How do you deserialize a JSON string into a data structure using JavaScript?
`JSON.parse(exampleJSONString)`
67
How do you store data in `localStorage`?
`window.localStorage.setItem('key', 'value');
68
How do you retrieve data from `localStorage`?
`window.localStorage.getItem('key');`
69
What data type can `localStorage` save in the browser?
`String` is the data type it can save, specifically in UTF-16
70
When does the `'beforeunload'` event fire on the `window` object?
It fires right before all contents on the page are about to be unloaded. (i.e closing the tab/refreshing)
71
What is a *method*?
A method is a function that is a property of an object.
72
How can you tell the difference between a method definition and a method call?
A method definition includes the code that makes the function work as intended. A method call only contains the function name and whatever arguments necessary.
73
Describe method definition syntax (structure).
A method definition is stored as a property, then its value is the function keyword with any desired parameters then the code block. Example: ``` var exampleObject = { exampleMethod: function (parameterA, parameterB) { } }; ```
74
Describe method definition syntax (structure).
A method definition is stored as a property, then its value is the function keyword with any desired parameters then the code block. Example: ``` var exampleObject = { exampleMethod: function () { return 'This is a method of the exampleObject object.'; } }; ```
75
Describe method call syntax (structure).
A method call is accessed with dot notation. Example: ``` exampleObject.exampleSum(1 , 2); ```
76
How is a method different from any other function?
It exists inside an object. It can only be called with the object.
77
What is the defining characteristic of Object-Oriented Programming?
It uses objects which store both data and behavior.
78
What are the four "principles" of Object-Oriented Programming?
[Abstraction](https://en.wikipedia.org/wiki/Abstraction_(computer_science)) [Encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)) [Inheritance](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) [Polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science))
79
What is "abstraction"?
Dealing with complex problems with simple means/simplifying an otherwise complex process into easy steps.
80
What does API stand for?
Application Programming Interface
81
What is the purpose of an API?
It gives programmers a way to interact with a system in a simplified way.
82
What is `this` in JavaScript?
`this` is an implicit parameter
83
What does it mean to say that `this` is an "implicit parameter"?
It means that even if it is not *explicitly* stated it *implicitly* exists.
84
*When* is the value of `this` determined in a function; **call time** or **definition time**?
Call time
85
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); } }; ```
`This` refers to nothing.
86
Given the above character object, what is the result of the following code snippet? Why? ``` character.greet(); ```
`It's -a-me, Mario!` will be the result. This is because of the `this` keyword being called in the greet method referencing the character.firstName property.
87
Given the above character object, what is the result of the following code snippet? Why? ``` var hello = character.greet; hello(); ```
`It's-a-me, undefined!` will be the result. This is because the `this` keyword cannot reference the character object since it is being assigned to a new variable, the method is not being called off the object. `This` is referencing the window object.
88
How can you tell what the value of this will be for a particular function or method definition?
You can't since the method/function isn't being called.
89
How can you tell what the value of this is for a particular function or method call?
Object to the left of the dot of the method call, if none is present it defaults to the window object.
90
What kind of inheritance does the JavaScript programming language use?
It uses prototype-based (prototypal) inheritance.
91
What is a prototype in JavaScript?
It is an object which stores general functionality that can be shared with many different objects that are patterned after it/similar to it.
92
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?
Each of those methods are stored on the prototype object of that kind of data type.
93
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
Within the prototype object of that data type.
94
What does the new operator do?
It creates a new instance of an object with the same [[Prototype]] as the specified constructor, executes the function with whatever arguments have been passed through to allow `this` to have a context of the new instance of the object, and then returns the object from the constructor function.
95
What property of JavaScript functions can store shared behavior for instances created with new?
The `prototype` property.
96
What does the instanceof operator do?
It checks if an object is an instance of a constructor function ( does the object use the same prototype )
97
What is a "callback" function?
A function that is passed as an argument of another function.
98
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?
`setTimeout()`
99
How can you set up a function to be called repeatedly without using a loop?
`setInterval()`
100
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
101
What do setTimeout() and setInterval() return?
They return unique IDs that can be used inside of `clearTimeout()` or `clearInterval()`
102
What is AJAX?
A group of technologies used to create asynchronous web applications
103
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
104
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
105
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
`load`
106
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Both are objects.
107
What is Node.js?
A set of JavaScript libraries that allows it to be used outside of the browser.
108
What can Node.js be used for?
It is used for backend such as network clients and servers.
109
What is a REPL?
It stands for Read-eval-print-loop, it basically takes what is input and evaluates what was input then prints out the result.
110
When was Node.js created?
2009 by Ryan Dahl
111
What is a computer process?
A collected list of instructions being executed by the computer. It can be viewed as the environment controlled by the OS in which a program is run, with memory allocated and CPU time allocated.
112
What is the process object in a Node.js program?
A global object representing the `node` process being run.
113
How do you access the process object in a Node.js program?
One can directly access the process object within their REPL or actual files
114
What is the data type of process.argv in Node.js?
It is an `array`
115
What is a JavaScript module?
A file that has functions/variables that can be exported to be processed in other files
116
What values are passed into a Node.js module's local scope?
exports, require, module, __filename, __dirname
117
What is the JavaScript Event Loop?
It is a mechanism in which asynchronous tasks are added to a queue and is executed if the stack is empty. (Stack is like the "job list" for the synchronous tasks)
118
What is different between "blocking" and "non-blocking" with respect to how code is executed?
Blocking: Each step must be finished in the order the code is called Non-blocking: The order in which steps are executed can be deferred/changed
119
What method is available in the Node.js fs module for writing data to a file?
`writeFile()`
120
Are file operations using the fs module synchronous or asynchronous?
Asynchronous
121
What are the three states a Promise can be in?
- pending (waiting for an async task to complete) - fulfilled (successfully completed task) - rejected (failed to complete task)
122
How do you handle the fulfillment of a Promise?
with .then()
123
How do you handle the rejection of a Promise?
with .catch()
124
What is `Array.prototype.filter` useful for?
It's useful for filtering an array to get the values that obey certain conditions. (NOTE: returns a *copy* instead of changing original)
125
What is `Array.prototype.map` useful for?
It's useful for modifying every value in the same manner and returning a copy.
126
What is `Array.prototype.reduce` useful for?
Combining related data (ie. account balances)
127
What does fetch() return?
It returns a promise that resolves to the `Response` object representing the response to our request from the specified resource.
128
What is the default request method used by fetch()?
`GET`
129
How do you specify the request method (GET, POST, etc.) when calling fetch?
One would specify it in the method property of the optional `options` parameter.