JavaScript Flashcards

1
Q

What is the purpose of variables?

A

the ability to store data that can change every time a script runs.

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 (keyword) varName(Variable Name/Identifier) = (Assignment Operator) number/string/boolean(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

use an equals sign(=) . aka 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

letters, numbers, dollar sign($), underscore(_),
cannot start with a number.
no dash(-) or period(.) in a variable name.
no keywords or reserved words.

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

an example would be that var alvaro and var Alvaro would be different variable names.
try not to create variables that have the same name doing different things.

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 when working with some type 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

when working with any arithmetic or something with a numeric value

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 tell whether true or false, also to tell JS which path to take.

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

Assignment Operator

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

you can use dot notation or square brackets.

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 something you place there and undefined is placed there by the browser.

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 help clarify whats being logged when you go back and review the code.

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

Give five examples of JavaScript primitives.

A

Numeric value, String, Boolean value, 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

always a numeric value.

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

What is string concatenation?

A

connecting two different strings or data that you want to be together.

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

its the addition operator, as well the concatenation operator.

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 value (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 addition assignment operator adds the value of the right side(right operand) and assigns the result to the variable.

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

What are arrays used for?

A

to store a list of related values.

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

Describe array literal notation.

A

var example = [‘one’, ‘two’, ‘three’];

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

How are arrays different from “plain” objects?

A

arrays are indexed such as 0, 1, 2, 3
objects cant use .length property.

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

What number represents the first index of an array?

A

0

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

What is the length property of an array?

A

length property tells you how many items are in the array.

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

How do you calculate the last index of an array?

A

var lastIndex = arrayName.length;
use dot notation = .length

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

Why do we log things to the console?

A

to be able to check the output and see if our code is working how it should.

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

What is a method?

A

A method is a function which is a property of an object.
instance methods
static methods

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

How is a method different from any other function?

A

a method needs to be attached to an object.

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

How do you remove the last element from an array?

A

pop( )

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

How do you round a number down to the nearest integer?

A

Math.floor( )

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

How do you generate a random number?

A

Math.random( )

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

How do you delete an element from an array?

A

.splice( )

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

How do you append an element to an array?

A

.push( )

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

How do you break a string up into an array?

A

.split( )

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

Do string methods change the original string? How would you check if you weren’t sure?

A

string methods do not change original string.
console.log the string

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

Is the return value of a function or method useful in every situation?

A

not necessarily if the function or method is coded correctly you dont really need it.

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

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

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

What is the purpose of a loop?

A

Loops check a condition, if the condition returns true the code block will run again, it repeats until condition returns false.
Loops offer a quick way to do something repeatedly. without writing something over and over.

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

What is the purpose of a condition expression in a loop?

A

the condition is checked before a loop runs, if the condition is true the code will run until the condition is false.

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

What does “iteration” mean in the context of loops?

A

each time the computer runs through a loop its considered an iteration.

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

When does the condition expression of a while loop get evaluated?

A

The condition is evaluated before executing the code block.
before every iteration.

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

When does the initialization expression of a for loop get evaluated?

A

the initialization gets evaluated once before the loop begins.

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

When does the condition expression of a for loop get evaluated?

A

condition expression gets evaluated before each loop iteration, if evaluates to true, statement is executed, if evaluates to false it exits the loop and goes to the first statement after the for { } construct

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

When does the final expression of a for loop get evaluated?

A

Final expression to be evaluated at the end of each loop iteration. generally used to update or increment the variable.

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

Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

Break statement terminates the current loop.

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

What does the ++ increment operator do?

A

The increment operator (++) increments (adds one to) its operand and returns a value.

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

How do you iterate through the keys of an object?

A

for in loop - iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

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

Why do we log things to the console?

A

to be able to see what our code is actually doing and make sure its doing what its supposed to.

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

What is a “model”?

A

as the browser loads the webpage it creates a model, that model is called the DOM Tree, it i s stored in the browsers memory, and consists of four main types of nodes.
- The Document Node
- Element Nodes
- Attribute Nodes
- Text Nodes.

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

Which “document” is being referred to in the phrase Document Object Model?

A

the html document or the whole webpage.

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

What is the word “object” referring to in the phrase Document Object Model?

A

the dom tree is made up of objects, each object represents a different part of the page, or it could be the node which is an object with methods and properties.

51
Q

What is a DOM Tree?

A

the model that the browser loaded is called the DOM Tree, consists of four main types of nodes, Document, Element, Attribute, and Text Nodes.

52
Q

Give two examples of document methods that retrieve a single element from the DOM.

A

getElementById( )
querySelector( )

53
Q

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

getElementsByClassName( )

54
Q

Why might you want to assign the return value of a DOM query to a variable?

A

if your script needs to use the same element more than once, This saves the browser looking through the DOM tree to find the same element again.

55
Q

What console method allows you to inspect the properties of a DOM element object?

A

console.dir( )
dir = directory.

56
Q

Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?

A

since the html loads top to bottom, it needs to load the elements before the javascript looks for elements.

57
Q

What does document.querySelector() take as its argument and what does it return?

A

argument: CSS selector syntax
returns: only the first of the matching elements.

58
Q

What does document.querySelectorAll() take as its argument and what does it return?

A

argument: CSS selector syntax
returns: all elements that match.

59
Q

What is the Dom

A

a javascript object that is modeling the html document.

60
Q

Why do we log things to the console?

A

to be able to see what our code is actually doing and make sure its doing what its supposed to.

61
Q

What is the purpose of events and event handling?

A
  • when a user interacts with an HTML web page, events trigger the javascript code.
  • three steps to trigger the javascript code is:
  • select the element node(s) you want the script to respond to
  • indicate which event on the selected element will trigger the response.
  • state the code you want to run when the event occurs.
62
Q

Are all possible parameters required to use a JavaScript method or function?

A

no

63
Q

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

Event Listener
DOM Element . addEventListener(‘event’, functionName, optional event flow).

64
Q

What is a callback function?

A

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.

65
Q

What object is passed into an event listener callback when the event fires?

A

event object -

66
Q

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

the place where the event took place. check it with console.log( ), check MDN for more information.

67
Q

What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())

A

first one fires once the event takes place
the bottom one takes place as soon as the page loads up.

68
Q

Does the document.createElement() method insert a new element into the page?

A

no it does not.

69
Q

How do you add an element as a child to another element?

A

appendChild( )

70
Q

What steps do you need to take in order to insert a new element into the page?

A

createElement
add textContent
append textContent to element

71
Q

What do you pass as the arguments to the element.setAttribute() method?

A

two strings the first being the attribute name, then the value of the attribute.

72
Q

What is the textContent property of an element object for?

A

represents the text content of the node and its descendants.

73
Q

Name two ways to set the class attribute of a DOM element.

A

setAttribute
.className

74
Q

What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A

re using the function and debugging if it doesnt work.

75
Q

How do you add an element as a child to another element?

A

appendChild( )

76
Q

What is the event.target?

A

returns the element that triggered the event.

77
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A
78
Q

What DOM element property tells you what type of element it is?

A

tagName

79
Q

What does the element.closest() method take as its argument and what does it return?

A

A string of valid CSS selector to match the Element and its ancestors against.

80
Q

How can you remove an element from the DOM?

A

remove( ) method on the object of the dom element we want to remove.

81
Q

If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?

A

add it to the nearest parent.

82
Q

What is JSON?

A

JavaScript Object Notation
JSON is a text-based data format following JavaScript object syntax

83
Q

What are serialization and deserialization?

A

serialization - Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.

deserialization - Deserialization is the exact opposite - Fetch a stream of bytes from memory or database and convert it back to the Object.

84
Q

Why are serialization and deserialization useful?

A

Its main purpose is to save the state of an object in order to be able to recreate it when needed.

Serialization is useful because you can transfer objects to main memory or across network, and deserialize them again later

85
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

JSON.stringify( )

86
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

JSON.parse( )

87
Q

How do you store data in localStorage?

A

storage.setItem( keyName, keyValue) will add the key to the storage object.

88
Q

How do you retrieve data from localStorage?

A

storage.getItem(keyName) will return that keys value

89
Q

What data type can localStorage save in the browser?

A

string

90
Q

When does the ‘beforeunload’ event fire on the window object?

A

The beforeunload event is fired when the window, the document and its resources are about to be unloaded.

91
Q

What is a method?

A

A method is a function which is a property of an object.

92
Q

How can you tell the difference between a method definition and a method call?

A

method definition is defining it within the object such as var doggo = { bark : function( ) { return ‘bark’ }} and calling it would be outside of the object such as doggo.bark( ).

93
Q

Describe method definition syntax (structure).

A

var doggo = {
bark : function ( ) {
return ‘woof’
}
}

94
Q

Describe method call syntax (structure).

A

object.property( param );

95
Q

How is a method different from any other function?

A
96
Q

What is the defining characteristic of Object-Oriented Programming?

A
97
Q

What are the four “principles” of Object-Oriented Programming?

A
98
Q

What is “abstraction”?

A
99
Q

What does API stand for?

A
100
Q

What is the purpose of an API?

A
101
Q

What is this in JavaScript?

A

this is an implicit parameter of all JavaScript functions.

102
Q

What does it mean to say that this is an “implicit parameter”?

A

implicit parameter means that 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.

103
Q

When is the value of this determined in a function; call time or definition time?

A

the value of this is determined when the function is called, not when it is defined.

104
Q

How can you tell what the value of this will be for a particular function or method definition?

A

within a definition you cannot tell what the value will be.

105
Q

How can you tell what the value of this is for a particular function or method call?

A

the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).

106
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based inheritance

107
Q

What is a prototype in JavaScript?

A

a JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be used by other objects.

create a tool that can be reused.

108
Q

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?

A

you would use the Object.setPrototype ( ) method, sets the prototype of a specified object to another object.

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.

109
Q

If an object does not have it’s own property or method by a given key, where does JavaScript look for it?

A

the prototype objects

looks up the prototypal chain to find it.

110
Q

What is a “callback” function?

A

A callback function is a function passed into another function as an argument

111
Q

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?

A

setTimeout( ) method

112
Q

How can you set up a function to be called repeatedly without using a loop?

A

setInterval( ) method.

113
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0

114
Q

What do setTimeout() and setInterval() return?

A

both return a timeoutId which is a number.

115
Q

What is AJAX?

A

Asynchronous JavaScript and XML,

allows you to request data from a server and load it without having to refresh the entire page.

116
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML,

117
Q

Which object is built into the browser for making HTTP requests in JavaScript?

A

.open( ) method?
initializes a newly-created request, or re-initializes an existing one.

118
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

the load event.

119
Q

Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

share a prototype object

120
Q

What is Array.prototype.filter useful for?

A

Whenever you need to filter elements out of an array.
makes it simpler to filter out items in an array, makes code more concise, removes need for for loops to check conditionals.

121
Q

What is Array.prototype.map useful for?

A

Whenever you want to call a function on every element in an array.
Whenever you need to generate a new array based on your original array?

122
Q

What is Array.prototype.reduce useful for?

A

Whenever you need to reduce an array to a single value.
useful for returning the sum or product of all elements in an array.

123
Q

In JavaScript, when is a function’s scope determined; when it is called or when it is defined?

A

When it is defined, Lexical Scope

124
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

Closures enable functions to remember their surroundings.