JavaScript Flashcards

1
Q

What is the purpose of variables?

A

It allows the program to store values

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

How do you declare a variable?

A

Begin with the keyword var followed by the variable’s name

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

Use “=” to set the variable equal to the value

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

Any letters, numbers, “$”, and “_”

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

Casing changes the name of a 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

To represent text values

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 represent numerical values used in calculation and indexing

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 represent logic values

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

It means to assign a value

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

Assign the variable with the new value

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 still considered an object while undefined is not

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

It gives more clarity to the console, which is helpful for 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, 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

number

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

What is string concatenation?

A

When two or more strings are combined to make a new string that is a combination of the original strings

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

It is both the arithmetic operator for addition and the concatenation operator for 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

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

It applies the + operator to the values on either side of the += and assigns the result to the variable on the left

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

What are objects used for?

A

They group together variables and functions to create a model of a real-world entity

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

What are object properties?

A

An object’s variables; a key/value pair

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

Describe object literal notation.

A

You define the object in curly braces {}, separate properties with commas, and define key/value pairs using a colon to separate them

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

Use the keyword delete followed by the object

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 object.key or bracket notation object[‘key’]

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

What are arrays used for?

A

Arrays are used for listing items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
Any number of values separated by commas, enclosed in square brackets
26
How are arrays different from "plain" objects?
The name of a property in the array is preset as a number
27
What number represents the first index of an array?
0
28
What is the length property of an array?
Gives the total number of objects in an array
29
How do you calculate the last index of an array?
array.length - 1
30
What is a function in JavaScript?
A portion of code that can be reused throughout a program
31
Describe the parts of a function definition.
the keyword "function", the optional function name, the parameters, and the code block
32
Describe the parts of a function call.
There is the function name followed by the arguments within the parentheses
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function call does not have the function keyword or code block
34
What is the difference between a parameter and an argument?
A parameter is the the general input used when defining the function, while an argument is the specific input used when calling the function
35
Why are function parameters useful?
They allow the function to take in values that can be manipulated across various cases
36
What two effects does a return statement have on the behavior of a function?
It provides a value that the function evaluates to, and terminates the reading of the function code block
37
Why do we log things to the console?
To verify our code works and help debug our code
38
What is a method?
A function defined within an object
39
How is a method different from any other function?
They can only be called on the object they are defined in
40
How do you remove the last element from an array?
.pop()
41
How do you round a number down to the nearest integer?
Math.floor(number)
42
How do you generate a random number?
Math.random()
43
How do you delete an element from an array?
.splice(start, deleteCount, item1, item2, ...)
44
How do you append an element to an array?
.push(element)
45
How do you break a string up into an array?
.split(separator)
46
Do string methods change the original string? How would you check if you weren't sure?
They do not; this can be confirmed by console logging the original string
47
Roughly how many string methods are there according to the MDN Web docs?
~50
48
Is the return value of a function or method useful in every situation?
No, for example .push() will return the new length of the array
49
Roughly how many array methods are there according to the MDN Web docs?
~30
50
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
mdn
51
Give 6 examples of comparison operators.
===, !==, >=, <=, >, less than sign
52
What data type do comparison expressions evaluate to?
boolean
53
What is the purpose of an if statement?
To write code that only executes conditionally
54
Is else required in order to use an if statement?
no
55
Describe the syntax (structure) of an if statement.
keyword if, condition statement, code block
56
What are the three logical operators?
&&, ||, !
57
How do you compare two different expressions in the same condition?
Connect them with a logical operator
58
What is the purpose of a loop?
It allows us to repeat behavior
59
What is the purpose of a condition expression in a loop?
It tells the loop whether or not to execute
60
What does "iteration" mean in the context of loops?
Each time the loop goes over the code is called an iteration
61
When does the condition expression of a while loop get evaluated?
At the beginning of each iteration
62
When does the initialization expression of a for loop get evaluated?
Once, at the start of the for loop
63
When does the condition expression of a for loop get evaluated?
At the start of each iteration of the for loop
64
When does the final expression of a for loop get evaluated?
At the end of each iteration of the for loop
65
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
66
What does the ++ increment operator do?
It evaluates the expression (variable + 1) and assign the result to the variable
67
How do you iterate through the keys of an object?
Use a for...in loop
68
What is one risk of writing a lot of code without checking to see if it works so far?
It is harder to pinpoint where a bug has occurred, since there is more code to consider
69
What is an advantage of having your console open when writing a JavaScript program?
You can log checkpoints in your code to the console to make sure everything is working properly
70
What is JSON?
A format for representing data as a JavaScript object that is in string form
71
What are serialization and deserialization?
Serialization is the process of turning data into a stream of bits, and deserialization is assembling these bits back into data
72
Why are serialization and deserialization useful?
It is how data is transferred and retrieved
73
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(obj)
74
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(str)
75
How do you store data in localStorage?
localStorage.setItem('key', 'value')
76
How do you retrieve data from localStorage?
localStorage.getItem('key')
77
What data type can localStorage save in the browser?
string
78
When does the 'beforeunload' event fire on the window object?
Right before the page is closed
79
How can you tell the difference between a method definition and a method call?
A method definition uses the "function" keyword
80
Describe method definition syntax (structure).
object.method = function (parameters) { };
81
Describe method call syntax (structure).
object.method(arguments)
82
What is the defining characteristic of Object-Oriented Programming?
Functionality is based around defining objects, which carry properties that can be modified using the object's methods
83
What are the four "principles" of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
84
What is "abstraction"?
Abstraction is creating a model of an entity and generally focusing on only the relevant properties of that entity
85
What does API stand for?
Application Programming Interface
86
What is the purpose of an API?
They are a prebuilt program that can be used by a programmer without needing to know the inner workings of the program
87
What is "this" in JavaScript?
It is an implicit parameter in a function/method definition that refers to the immediate object the method belongs to; note that a function definition is actually a method definition of the window object
88
What does it mean to say that "this" is an "implicit parameter"?
"This" is automatically a parameter of every function/method that does not need to be stated
89
When is the value of "this" determined in a function; call time or definition time?
call time
90
How can you tell what the value of "this" will be for a particular function or method definition?
For a method, it will be the object; for a function, it cannot be determined
91
How can you tell what the value of this is for a particular function or method call?
It is either the object immediately left of the method, or the window if no such object exists
92
``` 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); } }; ```
Nothing, since the method is not being called
93
``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; Given the above character object, what is the result of the following code snippet? Why? character.greet(); ```
"It's-a-me, Mario!" | Since the greet method is being called on the object character, this.firstName will be character.firstName
94
``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; 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!" The variable hello is being assigned the greet method of the character object and not the rest of the object, resulting in "this" being the window, which does not have a firstName property which makes this.firstName undefined
95
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance
96
What is a prototype in JavaScript?
A general version of an object that is used as a template for creating other objects using the Object.setPrototypeOf(obj, prototype) method
97
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?
They were created using a prototype that included those methods
98
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
In the __proto__ property of the object
99
What does the new operator do?
It returns a new instance of an object using the specified constructor function and arguments
100
What property of JavaScript functions can store shared behavior for instances created with new?
ThatFunction.prototype
101
What does the instanceof operator do?
Returns true if the prototype property of the second object appears anywhere in the prototype chain of the first object
102
What is a "callback" function?
A function name used as an argument, allowing it to be used by the outer function
103
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?
Using the setTimeout(function, delay) function
104
How can you set up a function to be called repeatedly without using a loop?
Using the setInterval(function, delay) function
105
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0; the callback function executes immediately when it comes up on the stack
106
What do setTimeout() and setInterval() return?
They return an integer that represents the timer's ID, which can be passed into the clearTimeout(ID)/clearInterval(ID) functions to cancel the respective timers
107
What is AJAX?
Technology that allows asynchronous functionality in the browser using network requests
108
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
109
Which object is built into the browser for making HTTP requests in JavaScript?
Objects made from the XMLHttpRequest() constructor
110
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
111
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They both contain the same prototype property in their respective prototype chains
112
What is a code block? What are some examples of a code block?
A section of code within curly braces that is generally modular (e.g. function code block, for/while loop code block, conditional code block)
113
What does block scope mean?
The section of code in which variables declared within the section will still persist
114
What is the scope of a variable declared with const or let?
The code block that they were declared in
115
What is the difference between let and const?
const makes the variable read-only
116
Why is it possible to .push() a new value into a const variable that points to an Array?
The method does not assign a new value (using the = operator) to the const variable
117
How should you decide on which type of declaration to use?
Always try to use const when you can, and only use let when the variable needs to be mutable
118
What is the syntax for writing a template literal?
The string is enclosed in backticks "`"
119
What is "string interpolation"?
It is defining a string using a template literal with imbedded variables
120
What is destructuring, conceptually?
A shorthand for getting properties from an object and assigning them to a new variable
121
What is the syntax for Object destructuring?
const { property1, property2 } = object;
122
What is the syntax for Array destructuring?
const [ valueAt0, valueAt1, ...remainingValues ] = array;
123
How can you tell the difference between destructuring and creating Object/Array literals?
The square brackets/curly braces are on the left side of the equal sign when you are destructuring
124
What is the syntax for defining an arrow function?
(...parameters) => {code block};
125
When an arrow function's body is left without curly braces, what changes in its functionality?
JavaScript assumes the expression to the right of the arrow will be returned from the function
126
How is the value of this determined within an arrow function?
Arrow functions do not bind their own `this`, and therefore adopt the value of `this` in the outer scope
127
What are the 4 major concepts that sets JavaScript apart from many other languages?
this, prototypal inheritance, event loop, closure
128
What is the JavaScript Event Loop?
It is the algorithm used for determining the task to be executed in the stack
129
What is different between "blocking" and "non-blocking" with respect to how code is executed?
A blocking task goes directly on the call stack, while a non-blocking task joins a queue that feeds into the call stack when the stack is clear
130
What are the three states a Promise can be in?
pending, fulfilled, rejected
131
How do you handle the fulfillment of a Promise?
You pass a callback function into the prom.then() method
132
How do you handle the rejection of a Promise?
You pass a second callback function into the prom.then() method, or pass a callback function into the prom.catch() method
133
What is Array.prototype.filter useful for?
For creating a new array that contain only elements of an array that meet a certain criteria
134
What is Array.prototype.map useful for?
When you want to transform every element in an array to make a new array
135
What is Array.prototype.reduce useful for?
When you want to iterate over an array and return a value based on the accumulated values of the array
136
What is "syntactic sugar"?
Additional syntax in a language that does not add to the language's functionality, but allows for easier reading
137
What is the typeof an ES6 class?
function
138
Describe ES6 class syntax.
``` class ClassName { constructor(...parameters) { } ``` method(...parameters) { } }
139
What is "refactoring"?
Changing code without changing it's behavior
140
How are ES Modules different from CommonJS modules?
It uses keywords import and export [default] instead of require() and module.exports
141
What kind of modules can Webpack support?
ES (ECMA Script), Common JS, AMD (Async module definition), Assets, and WebAssembly modules
142
What must the return value of myFunction be if the following expression is possible? myFunction()();
a function
143
``` What does wrap do? const wrap = value => ( ) => value; ```
Takes an argument `value` and returns a function that returns `value`
144
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
when it is defined
145
What allows JavaScript functions to "remember" values from their surroundings?
When a function is declared within a function, JS creates a closure that holds the local variables at the time of declaration; this closure can be referenced when the declared function is called