JavaScript Flashcards

1
Q

What is the purpose of variables?

A

variables are used to store data/information to be used in scripts/functions

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

How do you declare a variable?

A

var variableName;

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

variableName = variableValue;
using the assignment operator

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

Must start with letter, $, or _. Can contain letters, numbers, $’s, or _’s.

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

name and Name are not the same 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

Strings can be used for names, usernames, messages, etc. that need to be displayed.

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

Numbers are used for mathematical functions, counting, positioning on a screen, etc.

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

Booleans display values of true or false.

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

The = operator is 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

By setting a new value to the variable name using the = 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 can be used as a placeholder value for a variable as it is still an object.
Undefined is when no value has been assigned to a variable yet.

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

Labels make it more clear the reason that something is being logged to the console, or what the value being logged represents.
Helps with 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, and 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

pushing two string values together into one string value

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

addition of numbers or concatenation of 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 values

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

adds a value on the right to the variable on the left, then assigns the new value to that same variable

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

What are objects used for?

A

Grouping together sets of variables and their values as well as methods, typically a set of traits describing a real-world object.

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

What are object properties?

A

variables stored within an object

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

Describe object literal notation.

A

variableName = {
property: value,
property: 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

using the delete operator
delete objectName.objectProperty

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
objectName.objectProperty = propertyValue;

bracket notation
objectName[‘objectProperty’] = propertyValue;

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

What are arrays used for?

A

storing lists of values under a variable name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
arrayName = [item1, item2, item3] arrayName[1] item2
26
How are arrays different from "plain" objects?
they store data in order, allowing the use of the length property to pull values
27
What number represents the first index of an array?
0
28
What is the length property of an array?
the length property is the number of values that are stored in an array
29
How do you calculate the last index of an array?
subtract 1 from the length of an array arrayName.length - 1
30
What is a function in JavaScript?
a block of code that can be called upon by name to run multiple times, taking input and providing output.
31
Describe the parts of a function definition.
function functionName(parameters, if any) { lines of code being run; more lines of code being run; }
32
Describe the parts of a function call.
functionName(arguments);
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function definition contains the block of code that will be running. A function call is just the name of the function and the arguments being taken, not any code being run.
34
What is the difference between a parameter and an argument?
A parameter is in the function definition, and is a placeholder for what is needed in order for the function to run as designed. An argument is in the function call, and are the values needed to fulfill what the parameter is asking for.
35
Why are function parameters useful?
They are placeholders for information that will be passed as arguments in the function. When a function is called, the arguments will be directed to where all the placeholders are located within the function code to successfully run the function. Makes the function more flexible, being able to be used for more than one type of output.
36
What two effects does a return statement have on the behavior of a function?
1. It returns the value of a given variable to be used outside of the function block of code. 2. It prevents any other code from being run in the function's code block. (exits the function)
37
Why do we log things to the console?
To validate the expected results of a function
38
What is a method?
It is a function that is a property of an object
39
How is a method different from any other function?
They are specific to an object and must be called in reference to that object
40
How do you remove the last element from an array?
pop method
41
How do you round a number down to the nearest integer?
floor method
42
How do you generate a random number?
random method
43
How do you delete an element from an array?
pop, shift, or splice methods
44
How do you append an element to an array?
push or unshift methods
45
How do you break a string up into an array?
use the split method with commas
46
Do string methods change the original string? How would you check if you weren't sure?
They do not change the original string, they create a new string. This is why the result must be stored in a new variable to be used
47
Roughly how many string methods are there according to the MDN Web docs?
60 - 70 methods (look up string on MDN; left column)
48
Is the return value of a function or method useful in every situation?
It is only useful when values need to be assigned to a variable from a function, or when a function needs to be terminated.
49
Roughly how many array methods are there according to the MDN Web docs?
40 - 50 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.
>, <, >=, <=,
52
What data type do comparison expressions evaluate to?
boolean
53
What is the purpose of an if statement?
Allows for bifurcation in the code, so that different outcomes can be coded for based on the evaluation of an expression
54
Is else required in order to use an if statement?
No
55
Describe the syntax (structure) of an if statement.
if (expression) { code block } else { code block };
56
What are the three logical operators?
&&, !, || and, not, and or
57
How do you compare two different expressions in the same condition?
using the logical operators in the expression block
58
What is the purpose of a loop?
To repeat a block of code a specified (or unspecified) number of times
59
What is the purpose of a condition expression in a loop?
To ensure that the loop is able to stop
60
What does "iteration" mean in the context of loops?
An iteration is akin to each "step" in the loop series. If the loop repeats 10 times, there are 10 iterations
61
When does the condition expression of a while loop get evaluated?
Right before each pass through the loop, including before the first pass through the loop
62
When does the initialization expression of a for loop get evaluated?
One time before the loop begins
63
When does the condition expression of a for loop get evaluated?
Before each loop iteration, including the first iteration
64
When does the final expression of a for loop get evaluated?
At the end of each loop iteration
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?
increases a value by 1, and reassigns the value back to the variable
67
How do you iterate through the keys of an object?
Using a for...in loop for (var key in object) { console.log(key); OR console.log(object[key]); }
68
What is the event.target?
69
What is the affect of setting an element to display: none?
The elements and children are no longer displayed on the page, nor do they take up space
70
What does the element.matches() method take as an argument and what does it return?
71
How can you retrieve the value of an element's attribute?
getAttribute()
72
At what steps of the solution would it be helpful to log things to the console?
Anytime that code is expected to produce a return
73
If you were to add another tab and view to your HTML, but you didn't use event delegation, how would your JavaScript code be written instead?
add event listeners to each individual element
74
If you didn't use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
using an if else block to check each step of the code
75
What is JSON?
JavaScript Object Notation text-based format that represents data structures in JavaScript object syntax
76
What are serialization and deserialization?
When data is taken from JavaScript object syntax into JSON text format, and the reverse of that process
77
Why are serialization and deserialization useful?
It enables the transmission of data structures across networks
78
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
79
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
80
How do you store data in localStorage?
localStorage.setItem(keyName, keyValue)
81
How do you retrieve data from localStorage?
localStorage.getItem(keyName, keyValue)
82
What data type can localStorage save in the browser?
JSON strings
83
When does the 'beforeunload' event fire on the window object?
when the window, the document and its resources are about to be unloaded
84
What is a method?
a function that is a property of an object
85
How can you tell the difference between a method definition and a method call?
method definition will have a code block, method call will just be the name followed by parentheses
86
Describe method definition syntax (structure).
var object = { methodName: function (parameters) { code for function code for function } }
87
Describe method call syntax (structure).
object.method()
88
How is a method different from any other function?
It is attached to an object
89
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (properties) and behavior (methods)
90
What are the four "principles" of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
91
What is "abstraction"?
simplifying concepts and interfaces for the programming
92
What does API stand for?
application programming interface
93
What is the purpose of an API?
allows people to easily interface with an application and its data local storage is an example of an API
94
What is this in JavaScript?
an implicit parameter that can be used to access properties and values in a JavaScript object
95
What does it mean to say that this is an "implicit parameter"?
It does not need to be declared as a parameter in the function definition to be accessible
96
When is the value of this determined in a function; call time or definition time?
call time
97
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 the character object
98
Given the above character object, what is the result of the following code snippet? Why? character.greet();
'It's-a-me, Mario!' this.firstName accesses the character object and return the value of the firstName property
99
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! This occurs because the function in character.greet is being assigned to the variable hello, but the object is not being accessed by the function when called
100
How can you tell what the value of this will be for a particular function or method definition?
You can't tell until the function/method is called
101
How can you tell what the value of this is for a particular function or method call?
It depends on the object to the left of the dot when called. If there is value to the left of the dot, the value is window
102
What kind of inheritance does the JavaScript programming language use?
prototype-based
103
What is a prototype in JavaScript?
an object that contains properties and methods that can be used by other objects
104
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?
If those methods exist on a prototype, they can be borrowed by the other elements
105
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
In the prototype chain
106
What does the new operator do?
creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function. For a clearer answer, reference the MDN Description section for the new operator
107
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
108
What does the instanceof operator do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. This is used to check whether an object was created as an instance of a specific constructor
109
What is a "callback" function?
A function that is passed to another function
110
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() or setInterval()
111
How can you set up a function to be called repeatedly without using a loop?
setInterval()
112
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
Default time delay is 0, starting from when the page is loaded
113
What do setTimeout() and setInterval() return?
an Interval ID
114
What is AJAX?
A technique for loading data into part of a page without having to refresh the entire page
115
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
116
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
117
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
118
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
All JavaScript objects have prototypes with inherited methods
119
What must the return value of myFunction be if the following expression is possible? myFunction()();
120
What does this code do? const wrap = value => () => value;
121
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
122
What allows JavaScript functions to "remember" values from their surroundings?