JavaScript Flashcards

1
Q

What is the purpose of variables?

A

So the computer has a temporary location to 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

With the keyword ‘var’.

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

By 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

No special characters besides $ or _. No spaces.

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 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 a sequence of characters. Can be used when working with any kind of text.

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

For tasks that involve 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 help determine which part of a script should run.

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 assignment operator in JavaScript means the data on the right is being assigned to the variable on the left.

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 using 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 intentionally assigned to no value;

While undefined has not yet been assigned a value.

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

To make debugging easier.

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, symbol, bigInt.

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

Combining two or more strings into one.

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 and Concatenation.

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

Adds whatever number is on the right to the variable on the left, and then assigns that result to the variable.

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

What are objects used for?

A

To organize a set of variables and functions under one reusable item.

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

What are object properties?

A

A variable 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

Curly brackets with all properties and methods inside, separated by commas.

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

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

A

Using dot or bracket notation.

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

How to you remove a property from an object.

A

The delete operator.

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

What are arrays used for?

A

To store ordered lists of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe array literal notation.
[ ... , ... , ... , ... ]
26
How are arrays different from "plain" objects?
Arrays are ordered and can be accessed numerically.
27
What number represents the first index of an array?
0
28
What is the length property of an array?
Determines how many items are in the array. Based on memory indexes.
29
How do you calculate the last index of an array?
Length of the array minus one.
30
What is a function in JavaScript?
A "formula" used as a way for us to repeat code throughout our program without having to type that code out each time.
31
Describe the parts of a function definition.
function keyword, followed by an optional name, parameters enclosed in parentheses, code-block enclosed with curly-braces.
32
Describe the parts of a function call.
The function name, followed by parentheses and any necessary parameters.
33
When comparing them side-by-side, what are the differences between a function call and a function definition?
The code-block does not run when a function is defined, only when it is called.
34
What is the difference between a parameter and an argument?
A parameter is a placeholder variable inside a function definition. An argument is a variable passed to a function when it's called.
35
Why are function parameters useful?
So we can change the data we give to our functions.
36
What two effects does a return statement have on the behavior of a function?
It causes the function to end, and returns a value in it's place.
37
Why do we log things to the console?
To assist in debugging.
38
What is a method?
A function within an object.
39
How is a method different from any other function?
It is within an object.
40
How do you remove the last element from an array?
With the pop( ) method.
41
How do you round a number down to the nearest integer?
With the floor( ) method of the Math object.
42
How do you generate a random number?
With the random( ) method of the Math object.
43
How do you delete an element from an array?
With the splice( ) method.
44
How do you append an element to an array?
With the push( ) method.
45
How do you break a string up into an array?
With the split( ) method
46
Do string methods change the original string? How would you check if you weren't sure?
They do not; And you could console log the original string after to be sure.
47
Roughly how many string methods are there according to the MDN Web docs?
A LOT.
48
Is the return value of a function or method useful in every situation?
Not in every situation. Such as console.logs.
49
Roughly how many array methods are there according to the MDN Web docs?
A LOT.
50
Give 6 examples of comparison operators.
==, ===, , <=, >=
51
What data type do comparison expressions evaluate to?
Boolean values.
52
What is the purpose of an if statement?
To check a condition before running a segment of code.
53
Is else required in order to use an if statement?
No.
54
Describe the syntax (structure) of an if statement.
if ( condition ) { conditional code-block };
55
What are the three logical operators?
AND ( && ), OR ( || ), NOT ( ! )
56
How do you compare two different expressions in the same condition?
By using logical operators.
57
What is the purpose of a loop?
To repeat segments of code.
58
What is the purpose of a condition expression in a loop?
To check if the loop should continue.
59
What does "iteration" mean in the context of loops?
Each time the loop is run is 1 iteration.
60
When does the condition expression of a while loop get evaluated?
Before the loop runs.
61
When does the initialization expression of a for loop get evaluated?
The initialization is the first thing to get evaluated in a loop. Only happens once.
62
When does the condition expression of a for loop get evaluated?
Before the loop is run.
63
When does the final expression of a for loop get evaluated?
After the loop has run, unless otherwise specified.
64
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
65
What does the ++ increment operator do?
Increments the variable by one and adds it to itself.
66
How do you iterate through the keys of an object?
with a for...in loop
67
Which "document" is being referred to in the phrase Document Object Model?
To make things easier to understand/debug.
68
What is the word "object" referring to in the phrase Document Object Model?
JavaScript objects.
69
What is a DOM Tree?
A tree of objects created by JavaScript to interact with HTML.
70
Give two examples of document methods that retrieve a single element from the DOM.
.querySelector( ), .getElementById( )
71
Give one example of a document method that retrieves multiple elements from the DOM at once.
.querySelectorAll( )
72
Why might you want to assign the return value of a DOM query to a variable?
If you need to work with an element more than once.
73
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
74
What does document.querySelector() take as its argument and what does it return?
It takes a CSS Selector as its argument and returns the first match.
75
What does document.querySelectorAll() take as its argument and what does it return?
It takes a CSS Selector as its argument and returns a NodeList of all matches in the document.
76
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
So it runs after the HTML has loaded.
77
What is the purpose of events and event handling?
To detect things such as clicks and change what the user sees. (To make things interactive.)
78
What do [] square brackets mean in function and method syntax documentation?
It means optional.
79
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener( )
80
What is a callback function?
A function defined elsewhere in the code, passed as an argument.
81
What object is passed into an event listener callback when the event fires?
An object.
82
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 property is a reference to the object onto which the event was dispatched.
83
What is the className property of element objects?
A string containing all the classes of that element.
84
How do you update the CSS class attribute of an element using JavaScript?
By using the className property.
85
What is the textContent property of element objects?
A string encompassing all text inside an element.
86
How do you update the text within an element using JavaScript?
By assigning a new string to the textContent property of a variable.
87
Is the event parameter of an event listener callback always useful?
No. For instance when you are using the function as an argument.
88
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.
89
Why is storing information about a program in variables better than only storing it in the DOM?
It's much easier for the computer compared to having to search the DOM each time.
90
What does the transform property do?
Changes the way an item appears visually.
91
Give four examples of CSS transform functions.
Translate, skew, rotate, and scale.
92
The transition property is shorthand for which four CSS properties?
Transition-property, transition-duration, transition-timing-function, and transition-delay.
93
What event is fired when a user places their cursor in a form control?
focus
94
What event is fired when a user's cursor leaves a form control?
blur
95
What event is fired as a user changes the value of a form control?
input
96
What event is fired when a user clicks the "submit" button within a ?
submit
97
What does the event.preventDefault() method do?
If the event does not get explicitly handled, its default action should not be taken as it normally would be.
98
What does submitting a form without event.preventDefault() do?
Refresh the page.
99
What property of a form element object contains all of the form's controls.
elements property.
100
What property of form a control object gets and sets its value?
value
101
What is one risk of writing a lot of code without checking to see if it works so far?
You may have to go back and rewrite a lot of it.
102
What is an advantage of having your console open when writing a JavaScript program?
You can test as you go.
103
Does the document.createElement() method insert a new element into the page?
No, it just creates it in JavaScript.
104
How do you add an element as a child to another element?
With the appendChild( ) method.
105
What do you pass as the arguments to the element.setAttribute() method?
The attribute name and value.
106
What steps do you need to take in order to insert a new element into the page?
createElement( ), setAttribute( ) / .textContent, appendChild( )
107
What is the textContent property of an element object for?
To tell what text is inside the element.
108
Name two ways to set the class attribute of a DOM element.
using setAttribute( ) method, and the className( ) method.
109
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
It can be reused again and again. It simplifies things for the computer.
110
Give two examples of media features that you can query in an @media rule.
width, height, aspect-ratio
111
Which HTML meta tag is used in mobile-responsive web pages?
Viewport.
112
What is the event.target?
A reference to the object onto which the event was dispatched.
113
Why is it possible to listen for events on one element that actually happen to its descendent elements?
Because of event bubbling.
114
What DOM element property tells you what type of element it is?
event.target.tagName
115
What does the element.closest() method take as its argument and what does it return?
Searches the element and its parents to find a matching selector string. returns itself or the matching ancestor, or null if no matches are found.
116
How can you remove an element from the DOM?
With the remove( ) method.
117
What is the event.target?
A reference to the object onto which the event was dispatched.
118
What is the affect of setting an element to display: none?
Removes it from the layout.
119
What does the element.matches() method take as an argument and what does it return?
Takes a selector string as an argument, returns a boolean value.
120
How can you retrieve the value of an element's attribute?
Using the getAttribute( ) method.
121
At what steps of the solution would it be helpful to log things to the console?
Any time decisions are made or variables are assigned.
122
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?
You would have to add eventListeners to individual elements.
123
How to you store data in localStorage?
With the setItem( ) method.
124
How to you retrieve data from localStorage?
With the getItem( ) method.
125
What data type can localStorage save in the browser?
Strings.
126
When does the 'beforeunload' event fire on the window object?
When the window is about to be unloaded.
127
What is a method?
A function which is a property of an object.
128
How can you tell the difference between a method definition and a method call?
Method definitions are prefaced with the keyword function and followed by a code block
129
Describe method definition syntax (structure).
Name of the property first, followed by a colon, and then an anonymous function definition.
130
Describe method call syntax (structure).
Say the object first, then dot, then the method name.
131
How is a method different from any other function?
It is a property of an object.
132
What is the defining characteristic of Object-Oriented Programming?
Using objects to contain both data and behavior.
133
What are the four "principles" of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
134
What is "abstraction"?
Being able to work with complex things in seemingly simple ways.
135
What does API stand for?
Application Programming Interface
136
What is the purpose of an API?
To give programmers a way to interact with a system in a simplified, consistent fashion.
137
What is this in JavaScript?
A reference to the object where the function or method is being called.
138
What does it mean to say that this is an "implicit parameter"?
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.
139
When is the value of this determined in a function; call time or definition time?
Call time.
140
How can you tell what the value of this will be for a particular function or method definition?
'this' is not determined during definition.
141
What kind of inheritance does the JavaScript programming language use?
Prototype-based inheritance
142
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?
Because they are methods of its prototype.
143
If an object does not have its own property or method by a given key, where does JavaScript look for it?
On its prototype.
144
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.
145
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
146
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. The return value is a boolean value.
147
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.
148
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?
With the setTimeout( ) method.
149
How can you set up a function to be called repeatedly without using a loop?
By using the setInterval( ) method.
150
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0 milliseconds.
151
What do setTimeout() and setInterval() return?
A positive integer used as an ID to clear the timeout or interval.
152
What is a code block? What are some examples of a code block?
Code blocks are denoted by curly braces { }; Follows the condition in if/else statements and the parameter list in function definitions.
153
What does block scope mean?
It means a variable defined inside a block can only be referenced from within that same block.
154
What is the scope of a variable declared with const or let?
Block scope
155
What is the difference between let and const?
The const keyword creates block-scoped variables whose values can't be reassigned, while variables created with let are mutable.
156
Why is it possible to .push() a new value into a const variable that points to an Array?
Because the reference to the array is constant, but the values within that array are still mutable.
157
How should you decide on which type of declaration to use?
If a variable does not need to be reassigned, you should use a const; If it does, use let.
158
What must the return value of myFunction be if the following expression is possible? myFunction( )( );
A function.
159
``` What does this code do? const wrap = value => ( ) => value; ```
Defines an anonymous function receiving one parameter, value, which returns an anonymous function receiving no parameters and returning the variable value. This is all being assigned to the constant variable wrap.
160
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
When it is defined.