JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data

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

How do you initialize (assign a value to) a variable?

A

Assign a value after the assignment operator

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

What characters are allowed in variable names?

A

letters, numbers, dollar sign $, or an underscore _

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

What does it mean to say that variable names are “case sensitive”?

A

For the variable to be recognized, it needs to be entered with the same casing as it was when it was declared

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

What is the purpose of a boolean?

A

To determine if something is true or false, and to tell if script should run or not in conditionals

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

What does the = operator mean in JavaScript?

A

It is the assignment operator

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

How do you update the value of a variable?

A

Reassigning it without declaring it again, you can leave off the Var

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

What is the difference between null and undefined?

A

Null is intentional whereas undefined hasn’t been assigned yet

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

Give five examples of JavaScript primitives.

A

string, number, bigint, boolean, undefined, symbol, and null.

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

What is string concatenation?

A

joining together two or more strings to make a single value

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

What purpose(s) does the + plus operator serve in JavaScript?

A

addition for strings (concatenation) and numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q

What does the += “plus-equals” operator do?

A

Addition assignment, adds the values on both left and right operands

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

What are objects used for?

A

Group together a set of variables and functions to represent something such as a person or a car

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

What are object properties?

A

Variable within an object

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

Describe object literal notation.

A

Properties of the object are stored within curly braces

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

How do you remove a property from an object?

A

delete object.property

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

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

A

dot notation and bracket notation

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

What are arrays used for?

A

Storing a list of values

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

Describe array literal notation.

A

Putting the values of an array inside a bracket and separating each value with a comma.

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

The key for each value in an array is a number. The number is the location in the array.

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

A method that returns how many values are in an 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

array.length - 1

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

What is a function in JavaScript?

A

packages of code that can be called later in the program

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

Describe the parts of a function definition.

A
the function keyword
an optional name
zero or more parameters
a code block
an optional return statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Describe the parts of a function call.

A

the function name followed by the arguments replacing the parameters in ( )

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

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

The call is just the name and arguments

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

What is the difference between a parameter and an argument?

A

Parameteres are placeholders for arguments. The key thing to remember about parameters and arguments is that when we define a function, we declare parameters and that when we call a function, we pass it arguments.

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

Why are function parameters useful?

A

They let us pass data into the function

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

What two effects does a return statement have on the behavior of a function?

A
  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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is a method?

A

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

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

How is a method different from any other function?

A

A method is a property of an object

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

How do you remove the last element from an array?

A

the pop method

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

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

A

Math.floor method. The floor method of the Math object

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

How do you generate a random number?

A

Math.random method. The random method of the Math object

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

How do you delete an element from an array?

A

The splice method. it needs start and delete count parameters to know index to delete and how many to delete from there.

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

How do you append an element to an array?

A

The push method

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

How do you break a string up into an array?

A

The split method

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

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

A

they don’t, they can create new strings stored to new variables, you can check by console logging them and MDN. There is no way to change an existing string.

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

Give 6 examples of comparison operators.

A
== is equal too
=== strictly equal
!= is not equal too
!== strictly not equal
greater than
greater than or equals to
less than
less than or equals too
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is the purpose of an if statement?

A

to evaluate a conditional and execute code based on the evaluation
In simple terms, it is to make a decision.

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

Describe the syntax (structure) of an if statement.

A

key word, condition, opening curly brace, code to be executed, closing curly brace

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

What are the three logical operators?

A

&& and
|| or
!= not

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

How do you compare two different expressions in the same condition?

A

using logical operators

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

What is the purpose of a loop?

A

To check condition and run code certain number of times based on it

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

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

A

To give the conditions for the code to be executed and where to stop.

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

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

A

Each time the loop runs

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

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

A

before each iteration

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

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

A

before anything else

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

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

A

before each loop iteration

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

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

A

the end of each loop iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
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

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

What does the ++ increment operator do?

A

It is the incremental operator adding one to the counter, and assigns it to the i variable.

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

How do you iterate through the keys of an object?

A

With a For In Loop

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

Why do we log things to the console?

A

To test if they are working, and to look at our data

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

What is a “model”?

A

a representation of something

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

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

A

The HTML document

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

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

A

objects in the Javascript language

60
Q

What is a DOM Tree?

A

a tree of all the elements in the DOM

61
Q

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

A

getElementById()

querySelector()

62
Q

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

A

getElementsbyClassName()
getElementsbyTagName()
querySelectorAll()

63
Q

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

A

If you need to use the same element more than once

64
Q

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

A

console.dir

65
Q

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

A

The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them

66
Q

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

A

It takes a CSS selector and returns the first matching element

67
Q

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

A

It takes a CSS selector and it returns all elements that match.

68
Q

What is the purpose of events and event handling?

A

Events trigger code, and event handling tells what code it should be run when the event is handled.

69
Q

What do [] square brackets mean in function and method syntax documentation?

A

it means optional, it can be provided but doesn’t need to be provided

70
Q

What is a callback function?

A

A function called within a function

71
Q

What is a callback function?

A

A function called within a function used as an argument

72
Q

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

A

The event object that contains all the info about the event.

73
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A

The second one would run the event before the event is activated because the (), means the function is being called.

74
Q

What is the className property of element objects?

A

The className property of the Element interface gets and sets the value of the class attribute of the specified element.

75
Q

How do you update the CSS class attribute of an element using JavaScript?

A

element.className

76
Q

What is the textContent property of element objects?

A

The textContent property of the Node interface represents the text content of the node and its descendants.

77
Q

How do you update the text within an element using JavaScript?

A

element.textContent

78
Q

Is the event parameter of an event listener callback always useful?

A

No, you don’t always need info about the event.

79
Q

What does the transform property do?

A

The transform CSS property lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model.

80
Q

Give four examples of CSS transform functions.

A

rotate, translate(Y), skew, matrix, translate, scale, perspective

81
Q

The transition property is shorthand for which four CSS properties?

A

transition-delay
transition-duration
transition-property
transition-timing-function

82
Q

What event is fired when a user places their cursor in a form control?

A

focus

83
Q

What event is fired when a user’s cursor leaves a form control?

A

blur

84
Q

What event is fired as a user changes the value of a form control?

A

input

85
Q

What event is fired when a user clicks the “submit” button within a ?

A

submit

86
Q

What does the event.preventDefault() method do?

A

The Event interface’s preventDefault() method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

87
Q

What does submitting a form without event.preventDefault() do?

A

puts input into URL

88
Q

What property of a form element object contains all of the form’s controls.

A

Elements

89
Q

What property of form a control object gets and sets its value?

A

value

90
Q

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

A

No, you have to append it to the document first

91
Q

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

A

element.appendChild()

92
Q

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

A

name and value

93
Q

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

A

createElement()
createTextNode()
appendChild()

94
Q

What is the textContent property of an element object for?

A

it represents the text content of the element and can be used to change it

95
Q

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

A

element.setAttribute()

assign new className property

96
Q

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

A

To cut down on amount of code
makes it reusable

The function is dynamic, makes it easy to add new Pokemon to the pokedex

97
Q

Give two examples of media features that you can query in an @media rule.

A

width and height

98
Q

Which HTML meta tag is used in mobile-responsive web pages?

A

Meta viewport

It is put in head along with other meta tags in title.

99
Q

What is the event.target?

A

The target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

Element the event originated from.

100
Q

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

A

Event bubbling

101
Q

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

A

tagName

102
Q

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

A

It takes a selector and returns the closest ancestor to the selector

103
Q

How can you remove an element from the DOM?

A

ChildNode.remove()

104
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

event delegation

105
Q

What is the affect of setting an element to display: none?

A

it makes it invisible and removes it from the document flow

106
Q

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

A
var result = element.matches(CSSselectorString);
it returns a boolean
107
Q

How can you retrieve the value of an element’s attribute?

A

let attribute = element.getAttribute(attributeName);

108
Q

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?

A

you would have to add multiple event listeners to each tab element

109
Q

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?

A

write it all over and over again

110
Q

What is JSON?

A

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications

111
Q

What are serialization and deserialization?

A

Serialization is compressing something to a string to be sent
Deserialization is unpacking a string into an object that was compressed

112
Q

Why are serialization and deserialization useful?

A

Sending data , and to put things in correct serialized order

113
Q

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

A

stringify

114
Q

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

A

parse

115
Q

How to you store data in localStorage?

A

localStorage.setItem(key,value)

116
Q

How to you retrieve data from localStorage?

A

localStorage.getItem(key)

117
Q

What data type can localStorage save in the browser?

A

strings

118
Q

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

A

when leaving the page, or anything else that will end current browsing session

119
Q

What is a method?

A

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

120
Q

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

A

a method call will have () without definition following it

121
Q

Describe method definition syntax (structure).

A

inside the object literal assigned to a variable, list the property, followed by the function key word, then parameters, then curly braces for function definition.

122
Q

Describe method call syntax (structure).

A

object.property(parameters)

123
Q

How is a method different from any other function?

A

a method can only be called as a property of an object

124
Q

What is the defining characteristic of Object-Oriented Programming?

A

If you remember anything about OOP, it should be: objects can contain both data (as properties) and behavior (as methods).

It is about pairing data and behavior.

125
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

126
Q

What is “abstraction”?

A

being able to work with (possibly) complex things in simple ways, think of light switches for example

127
Q

What does API stand for?

A

application programming interface

128
Q

What is the purpose of an API?

A

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.

129
Q

What is this in JavaScript?

A

this is an implicit parameter of all JavaScript functions.

referencing to the object it is currently in. If not in an object, then it is referencing the window

130
Q

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

A

It is implied without being listed as a parameter,

it is a variable value available in a function that you didn’t declare yourself.

131
Q

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

A

Call time

132
Q

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

A

You don’t know till it is called

133
Q

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

A

The value to the left of the dot, if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.

134
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype based or prototypal

135
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.

136
Q

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?

A

prototypes

137
Q

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

A

on the prototype object

138
Q

What does the new operator do?

A
  1. makes new blank object
  2. Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
  3. Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
  4. Returns this if the function doesn’t return an object.
139
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

prototype

140
Q

What does the instanceof operator do?

A

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.

141
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.

142
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()

143
Q

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

A

setInterval()

144
Q

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

A

The default value is 0 milliseconds.

145
Q

What do setTimeout() and setInterval() return?

A

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.

The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to WindowOrWorkerGlobalScope.clearInterval() to cancel the interval.