JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Allows you to store data and access them later.

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

How do you declare a variable?

A
  1. variable keyword
  2. variable name
  3. assignment operator (=)
  4. variable value
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

=

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, digits, _, $, and it cannot start with a number
(ex: cat1 is okay but not 1cat)

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

uppercase and lowercase matters in JavaScript.
(ex. JavaScript != javascript)
precision is mandatory

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

Storing some 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

Stored when we need to manipulate mathematical information

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

It is important for conditions. Able to represent if something is or isn’t.
Their purpose is to make decisions.

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 is the assignment operator.
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

When updating, you do not need the variable keyword, but still need 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

undefined ==> a value that is not actually there. Means empty
null ==> null is a value that is assigned. It is done on purpose.

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

Having labels makes it clear what we are working with.
Sometimes, there are multiple console.logs and you would need to differentiate.

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

Give five examples of JavaScript primitives.

A
  1. String
  2. Numbers
  3. Booleans
  4. Null
  5. 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

The process of joining together two or more strings to create one new string. Uses the + (plus operator)

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

The plus operator can be used for string concatenation and as an arithmetic operation (adding).

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

A boolean
(true or false)

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

The += is an assignment operator that sums up the left and right operand values then assign the obtaining result to the left operand.
It will increment your sum variable with the amount next to it.

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

What are objects used for?

A

Objects group together a set of variables and functions to create a model of something you would recognize from the real world

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

What are object properties?

A

If a variable is part of an object, it is called a property. Properties tell us about the object, such as the name of a hotel or the number of rooms it has.

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

Describe object literal notation.

A

The object is the curly braces and their contents. Separate each key from its value using a colon. Separate each property and method with a comma (but not after the last 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

Use the delete operator and assign the property using either the dot or bracket notation.

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 and bracket notation.
Ex. student.firstName || student[‘firstName’]

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

What are arrays used for?

A

Arrays store a list of values.
You should consider using an array whenever you are working with a list or a set of values that are related to each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
The values are assigned to the array inside a pair of square brackets[ ], and each value is separated by a comma. The values in the array do not need to be the same data type, so you can store a string, a number and a Boolean all in the sam array.
26
How are arrays different from "plain" objects?
Arrays are a special type of object. They hold a related set of key/value pairs (like all objects), but the key for each value of an array is its index number. They tell you the length. To add data, you would need a method (push). Arrays have an order.
27
What number represents the first index of an array?
0
28
What is the length property of an array?
Each array has a property called length, which holds the number of items in the array. ( ex. array.length)
29
How do you calculate the last index of an array?
You take the length property of the array and subtract by 1.
30
What is a function in JavaScript?
Packed up code that can be reused throughout a program. A set of statements that performs a task or calculates a value.
31
Describe the parts of a function definition.
1. The function keyword to begin the creation of the function 2. An optional name 3. A comma-separated list of zero or more parameters, surrounded by ( ) 4. The start of the function's code block, indicated by an { opening curly brace 5. An optional return statement. 6. The end of the function's code block, as indicated by a } closing curly brace
32
Describe the parts of a function call.
1. The function's name 2. A comma-separated list of zero or more arguments surrounded by ( )
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call is just the function name with the argument. Function definition has the function keyword and the code block.
34
What is the difference between a parameter and an argument?
A parameter is a placeholder. It is a variable whose value is not known until we call the function and pass an argument. When the function's code block is run, the parameter will be holding the value of the argument. When we define a function, we declare parameters When we call a function, we pass it arguments
35
Why are function parameters useful?
It can be used as a placeholder for when the arguments will eventually be passed. Also, the parameter can be used within the code block when making the function
36
What two effects does a return statement have on the behavior of a function?
1. Causes the function to produce a value we can use in our program 2. Prevents any more code in the function's code block from being run
37
Why do we log things to the console?
The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code.
38
What is a method?
A method is a function which is a property of an object.
39
How is a method different from any other function?
Methods are associated with an object, while a function is not.
40
How do you remove the last element from an array?
Using the pop() method Array.prototype.pop()
41
How do you round a number down to the nearest integer?
using the floor method of the Math object. Math.floor()
42
How do you generate a random number?
by calling the random() method of the Math object. Math.random()
43
How do you delete an element from an array?
By calling the splice() method Array.prototype.splice() splice(start, deleteCount)
44
How do you append an element to an array?
By calling the push() methods. Array.prototype.push() append = Put it on the end
45
How do you break a string up into an array?
By calling the split() method String.prototype.split()
46
Do string methods change the original string? How would you check if you weren't sure?
No. Strings are immutable. You would check logging the value to the console.
47
Roughly how many string methods are there according to the MDN Web docs?
50 string methods
48
Is the return value of a function or method useful in every situation?
No. functions and methods are only useful in specific situations
49
Roughly how many array methods are there according to the MDN Web docs?
41 array methods
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.
1. Less than (<) 2. Greater than (>) 3. Less than or equal too (<=) 4. Greater than or equal too (>=) 5. Strict equality operator (===) 6. Strict inequality operator (!==)
52
What data type do comparison expressions evaluate too?
Booleans
53
What is the purpose of an if statement?
To make decisions in the code
54
Is else required in order to use an if statement?
No
55
Describe the syntax (structure) of an if statement.
1. if keyword 2. condition 3. opening curly brace 4. code to execute if value is true 4. closing curly brace
56
What are the three logical operators?
1. Logical AND (&&) 2. Logical OR (||) 3. Logical NOT (!)
57
How do you compare two different expressions in the same condition?
You use the logical operators (AND, OR) &&, ||
58
What is the purpose of a loop?
Loops are a programming element that repeat a portion of code a set number of times until the desired process is complete.
59
What is the purpose of a condition expression in a loop?
If the value of conditionExpression is true, the loop statements execute. Otherwise, the for loop terminates.
60
What does "iteration" mean in the context of loops?
Iteration is the process of repeating steps. How many times the loop gets repeated.
61
When does the condition expression of a while loop get evaluated?
The condition test occurs before statement in the loop is executed. Occurs before each iteration
62
When does the initialization expression of a for loop get evaluated?
One time before everything.
63
When does the condition expression of a for loop get evaluated?
condition expression is evaluated after the initialization expression, and then it happens before the final expression after each iteration.
64
When does the final expression of a for loop get evaluated?
Before the condition and after the code block
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?
Adds one to the variable it is applied too and saves it in that variable
67
How do you iterate through the keys of an object?
Use a for in loop
68
Why do we log things to the console?
So that we can see all the properties within the object Helps with debugging.
69
What is a "model"?
A series of objects that represents a web page that the browser saves in memory. A model of the HTML webpage
70
Which "document" is being referred to in the phrase Document Object Model?
The HTML Document
71
What is the word "object" referring to in the phrase Document Object Model?
A javascript object that is modeling the HTML document
72
What is a DOM Tree?
It is a representation of the entire webpage. Every element, attribute, and piece of text in the HTML is represented by its own DOM node. Everything below the document node is called the DOM Tree.
73
Give two examples of document methods that retrieve a single element from the DOM.
1. querySelector() 2. getElementById()
74
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll()
75
Why might you want to assign the return value of a DOM query to a variable?
You can use dot notation to access the content within that node since the DOM is made up of all objects
76
What console method allows you to inspect the properties of a DOM element object?
console.dir() dir = directory
77
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
It may slow down the page rendering speed since it will execute the javascript code on the spot. HTML document loads from top to bottom. Part of HTML will not be created yet if you are using JavaScript to work with the DOM
78
What does document.querySelector() take as its argument and what does it return?
document.querySelector() takes in string as an argument and returns the first element that matches that selector.
79
What does document.querySelectorAll() take as its argument and what does it return?
document.querySelectorAll() takes in a string as an argument and returns returns a nodeList object of all the elements that the string represents.
80
Why do we log things to the console?
Helps with debugging and checking to see if there are any mistakes in our code
81
What is the purpose of events and event handling?
Allows users to interact with the webpage. For us to be aware when a certain action happens. Our job as developers is to have something happen when that action happens
82
Are all possible parameters required to use a JavaScript method or function?
No
83
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener()
84
What is a callback function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
85
What object is passed into an event listener callback when the event fires?
A function that we have made will be called when the event fires The event object.
86
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
The event.target is the piece of HTML where the event originated. We can check by logging it into the console. We can get more information by googling online using MDN
87
What is the difference between these two snippets of code? 1. element.addEventListener('click', handleClick) 2. element.addEventListener('click', handleClick())
In the first snippet of code, the handleClick function is being called when the click event happens. In the second snippet of code, the handleClick function is being called when the page loads.
88
What is the className property of element objects?
The className property of the Element interface gets and sets the value of the class attribute of the specified element. Property containing that class attribute of that element.
89
How do you update the CSS class attribute of an element using JavaScript?
Using the className property
90
What is the textContent property of element objects?
The textContent property of the Node interface represents the text content of the node and its descendants.
91
How do you update the text within an element using JavaScript?
Using the textContent property using the assignment operator
92
Is the event parameter of an event listener callback always useful?
No
93
Would this assignment be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
More complicated since a variable can be reused
94
Why is storing information about a program in variables better than only storing it in the DOM?
Because variables are shorter to write and you can use them whenever you need to. Better to use data from the language you are using.
95
What is JSON?
JavaScript Object Notation (JSON) It is a standard text-based format for representing structured data based on JavaScript object syntax
96
What are serialization and deserialization?
Serialization: the process of turning an object in memory into a stream(one line) of bytes so you can do stuff like store it on disk or send it over the network. Deserialization: is the reverse process: turning a stream of bytes into an object in memory.
97
Why are serialization and deserialization useful?
Serialization turns the object into a string, which is useful for when you want to transmit data across a network. Although serialized data resembles javascript object literal syntax, it can be used independently from javascript. Deserialization is useful because it can take serialized data and return it into object form.
98
How do you serialize a data structure into a JSON string using JavaScript?
Use the JSON.stringify() method
99
How do you deserialize a JSON string into a data structure using JavaScript?
Use the JSON.parse() method
100
How do you store data in localStorage?
setItem method of the local storage object
101
How do you retrieve data from localStorage?
getItem method of the localStorage object
102
What data type can localStorage save in the browser?
A string
103
When does the 'beforeunload' event fire on the window object?
When the window is about to be closed
104
What is a method?
A method is a function which is a property of an object.
105
How can you tell the difference between a method definition and a method call?
Method definition: Includes a property name, the function key word, parameters, and function code block. Method call: Only includes the object name with the method using dot notation and the arguments inside parentheses.
106
Describe method definition syntax (structure).
Method definition: Define an object literal. Within the object literal, includes a property name being define. Within the value is the function key word, parameters inside parentheses, and the function code block. ex. var calculator = { add: function (x, y) { return x + y; }
107
Describe method call syntax (structure).
Method call: Only includes the object name with the method using dot notation and the arguments inside parentheses. ex. calculator. add(5, 5);
108
How is a method different from any other function?
A method is a function that is ONLY the property of an object
109
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data (as properties) and behavior (as methods).
110
What are the four "principles" of Object-Oriented Programming?
1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism
111
What is "abstraction"?
The most important part of the term "abstraction" boils down to being able to work with (possibly) complex things in simple ways. Light switch analogy.
112
What does API stand for?
Application Programming Interface (API)
113
What is the purpose of an API?
The purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
114
What is this in JavaScript?
Implicit parameter that allows up to call a variable within the code block
115
What does it mean to say that this is an "implicit parameter"?
A value that is just given to each function. It is always present. it is available in a function's code block even though it was never included in the function's parameter list or declared with var.
116
When is the value of this determined in a function; call time or definition time?
Call time
117
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); } };
The value of this is nothing because the greet method has not been called yet.
118
How can you tell what the value of this will be for a particular function or method definition?
We wouldn't know what the value of this is when the method is being defined. You would only know when the method is called. 'This' is a placeholder and we never know the value of the placeholder.
119
How can you tell what the value of this is for a particular function or method call?
Whatever is on the left of the dot.
120
What kind of inheritance does the JavaScript programming language use?
prototype/prototypal-based inheritance
121
What is a prototype in JavaScript?
The prototype is an object that is associated with every functions and objects by default in JavaScript
122
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?
It turns out arrays don't actually have the methods that you've come to know. Instead, those methods are defined on a "prototype" object and arrays simply borrow those methods when they're needed.
123
If an object does not have its own property or method by a given key, where does JavaScript look for it?
In the prototype object
124
What does the new operator do?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
125
What property of JavaScript functions can store shared behavior for instances created with new?
The prototype property
126
What does the instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
127
What is a "callback" function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
128
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?
Use the setTimeout method
129
How can you set up a function to be called repeatedly without using a loop?
Use the setInterval method
130
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
default time delay of 0
131
What do setTimeout() and setInterval() return?
setInterval returns the intervalID and setTimeout returns the timeoutID. Which will always be a positive integer
132
What is AJAX?
Ajax, which initially stood for Asynchronous JavaScript And XML, is a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest. Loading data to a part of the page without a need for reloading.
133
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
134
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest()
135
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
The 'load' event is fired when an XMLHttpRequest transaction completes successfully.
136
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They share a prototype
137
What is Array.prototype.filter useful for?
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
138
What must the return value of myFunction be if the following expression is possible? myFunction()();
It would return the return value of the function that is inside of myFunction.
139
What does this code do? const wrap = value => () => value;
When wrap is called, it would return the function that holds value
140
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
When it is defined
141
What allows JavaScript functions to "remember" values from their surroundings?
Closures
142
What are closures?
It is a backpack for functions that remember values that it might need later. If the value of the outer function is used in the inner function. The value in the outer function is remembered when the value is used in the inner function.