JavaScript Flashcards

(172 cards)

1
Q

what is the purpose of a variable?

A

to store information to be referenced to

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

How do you declare a variable?

A

variable keyword and variable name

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

with an = sign (assignment operator) and a variable value after it

var nameOfVar = value (string, number, boolean data type)

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, or underscore

but a number cannot be the first character

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

when you call the variable name, you have to call it by its exact value or the computer won’t recognize it

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 collection of characters (could be a letter, number, or a backslash)

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

to store numbers

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 determine which code should run?

to store true/false value

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

variable assignment

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

call the variable name, assignment operator, and the new variable value

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 an assigned value– it means there is nothing in the variable

undefined means a variable has been declared but not defined with values yet

as a developer, you never want to assign a variable as undefined

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

so you can tell which variables are being logged and in what order– this is especially helpful when you need to debug

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

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 purpose(s) does the + plus operator serve in JavaScript?

A

to concatenate strings or add numbers

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

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

A

adds/concatnates value from the right to the left variable

the current value of that variable, bring in some value, add that on to the current value, and the result of that is the new value

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

What is string concatenation?

A

the process of joining multiple strings together

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

What are objects used for?

A

used to group together a set of variables and functions to create a model

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

What are object properties?

A

a variable– properties tell us about the object

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

Describe object literal notation.

A

its an array of key-value pairs with a colon separating the keys and values and a comma after every key-value pair (except for the last)

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

you use the delete keyword followed by the object name and the property name

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 or bracket notation

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

What are object methods?

A

a function– methods represent tasks that are associated with the object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What are arrays used for?
arrays are used to store a list of values that are related to each other
26
Describe array literal notation.
values are assigned to the array inside a pair of square brackets and each value is separated by a comma
27
How are arrays different from "plain" objects?
like objects, arrays hold a key value pairs, but with arrays, the key for each value is its index number array is a list of items and objects are collections of individual items? objects are a collection of data with individual name of properties with no set order and arrays are in a set order and are a list of items arrays hold data that are similar
28
What number represents the first index of an array?
0
29
What is the length property of an array?
the amount of items in the array
30
How do you calculate the last index of an array?
array length minus 1
31
What is a function in JavaScript?
it's essentially a block of code or statement you write that performs a task or calculates a value and it can be used multiple times
32
Describe the parts of a function definition.
- function keyword - function name - parentheses with parameters inside - curly braces - inside curly braces, you have the function code block
33
Describe the parts of a function call.
- name of the function - open and close parentheses - inside the parentheses, you have arguments
34
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function definition is a lot longer than a function call with all the tasks it needs to perform, however, a function definition alone is not going to perform any action unless the function call calls the function
35
What is the difference between a parameter and an argument?
parameter is essentially a placeholder for when we call the function and pass in an argument
36
Why are function parameters useful?
parameters allow us to pass instructions into a function
37
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 | 2) prevents any more code in the function code block from being run
38
Why do we log things to the console?
it informs what the code is doing and alerts you if there's an issue-- so logging things to the console is great for debugging or when you don't understand code block to see what the code we're writing is doing
39
What is a method?
a method is a function which is a property of an object a function stored in a property of an object
40
How is a method different from any other function?
a method is attached to an object in order to call a method, you have to attach the function name to their object using dot notation, and to call a regular function, you use its name
41
How do you remove the last element from an array?
pop() method
42
How do you round a number down to the nearest integer?
floor() method
43
How do you generate a random number?
Math.random() (from 0 - .9999) what's the purpose? treat it as a percentage, that will get multiplied with your range value random acts as a decimal
44
How do you delete an element from an array?
splice() method arguments: (index, how many elements to remove)
45
How do you append an element to an array?
push()
46
How do you break a string up into an array?
split()
47
Do string methods change the original string? How would you check if you weren't sure?
string methods do not change your original string you can check by logging your value to the console strings are immutable, you cannot change the original string
48
Roughly how many string methods are there according to the MDN Web docs?
a lot
49
Is the return value of a function or method useful in every situation?
no like the push method, you dont need to know the length of it
50
Roughly how many array methods are there according to the MDN Web docs?
a lot
51
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN, official docs, it has all the documents you need
52
Give 6 examples of comparison operators.
less than, greater than, less than or equal to, greater than or equal to, strictly equal, strictly not equal
53
What is the purpose of an if statement?
to test conditions, if the condition is true, the subsequent code statement runs to make decicions
54
Is else required in order to use an if statement?
no | only use else if you need to
55
Describe the syntax (structure) of an if statement.
``` if keyword open and close parenthesis a condition inside the parentheses open and curly braces code to be executed inside the curly braces if condition is true ```
56
What are the three logical operators?
logical and operator logical or operator logical not operator
57
How do you compare two different expressions in the same condition?
expression one on one side expression two on the other side and a logical operator in between follow by another pair of parenthesis to wrap the two expressions by using logical operaters
58
What data type do comparison expressions evaluate to?
boolean
59
What is the purpose of a loop?
to repeat similar code a number of times to repeat code and when to stop repreating code
60
What is the purpose of a condition expression in a loop?
it lets the loop statement know whether it should be executed we can think of condition as a break, if its truthy we continue if its falsy we stop
61
What does "iteration" mean in the context of loops?
repeat?
62
When does the condition expression of a while loop get evaluated?
first
63
When does the initialization expression of a for loop get evaluated?
first, before anything, and only once
64
When does the condition expression of a for loop get evaluated?
second, your condition run betore each iteration
65
When does the final expression of a for loop get evaluated?
last, after the code block has ran? it is run after the code block
66
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
67
What does the ++ increment operator do?
adds one to its operand, increments the variable by one
68
How do you iterate through the keys of an object?
for in loop
69
Why do we log things to the console?
to see what the code is doing and to understand what is going on
70
What is a "model"?
a set of rules that define what type of content each element is allowed to have?? a dom tree? a representation of something else
71
Which "document" is being referred to in the phrase Document Object Model?
HTML
72
What is the word "object" referring to in the phrase Document Object Model?
object is referring to the data type?
73
What is a DOM Tree?
the dom tree is an element plus all of its containing elements including the child elements, etc the dom tree is an element holding all other elements a DOM tree is a tree of elements with the document node at the top (root) which points to the element node which in turn points to its child element nodes (head and body) and so on from each of those elements, you can navigate the DOM structure and more to different nodes
74
Give two examples of document methods that retrieve a single element from the DOM.
querySelector() and getElementById()
75
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll()
76
Why might you want to assign the return value of a DOM query to a variable?
to make it easier to access the value later on
77
What console method allows you to inspect the properties of a DOM element object?
console.dir() -- displays an interactive list of the properties of the specified JavaScript object the output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects use it when you want to see the data of the dom elements?
78
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
it gives the HTML time to load before any of the JS loads which can prevent errors, and speed up website response time
79
What does document.querySelector() take as its argument and what does it return?
takes in a selector that describes the element your querying for and returns the HTML element object representing the first element of the specified selectors
80
What does document.querySelectorAll() take as its argument and what does it return?
takes in a selector and return a node list (an object of a collection of nodes) of the document's elements that match the specified group of selectors
81
Why do we log things to the console?
to see what the code is doing
82
What is the purpose of events and event handling?
events and event handling trigger the code when specific events occur from the user (such as when the user clicks on a button) without events and event handle, your website is static (not changing)
83
What do [] square brackets mean in function and method syntax documentation?
it means that that piece is OPTIONAL
84
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener()
85
What is a callback function?
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
86
What object is passed into an event listener callback when the event fires?
event object (such as event.target, event.pageX, event.pageY..)
87
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
you can check on MDN for answer it returns the node/element that was targeted by the function get the element that triggered a specific event the target event property returns the element that triggered the event the html element that triggered the event
88
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
the parentheses are omitted where the function is called because they would indicate that the function should run as the page loads (rather than when the event fires) the first code-- handleClick function will run when 'click' occur vs the second code--handleClick() will run right away
89
What is the className property of element objects?
a string value that contain a list of all the classes that applied to that element
90
How do you update the CSS class attribute of an element using JavaScript?
assignment operator? you'd need the dom element of that class? when you are updating a className, you are updating the className attribute?
91
What is the className property of the element objects?
it gets and sets the value of the class attribute of the specified element
92
Is the event parameter of an event listener callback always useful?
no, not always usefull
93
Is the event parameter of an event listener callback always useful?
no, not always useful
94
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, we'd have to dgo back to the dom to retrieve the counter number and reassignn it to the textContent
95
What event is fired when a user places their cursor in a form control?
focus event
96
What event is fired when a user's cursor leaves a form control?
blur event
97
What event is fired as a user changes the value of a form control?
input event | change event is similar, but stick to input event for now
98
What event is fired when a user clicks the "submit" button within a ?
submit event
99
What does the event.preventDefault() method do?
tells the browser that if the event does not get explicitly handled, its default action should not be taken as it normally would be prevent default method allows you to prevent the default behavior from occurring on an event ex: when a link (anchor tag) is involved, the behavior default is to navigate to that new URL, but using the method, we're able to stop the browser from redirecting the page
100
What does submitting a form without event.preventDefault() do?
the browser would reload the page and submit the data for you and refreshes the page (this is if the form does not have an action attribute (as if action="insert-link"), it'll just reload the page) (with the method, if there's an action attribute within our form element, it will prevent the browser to send the data over to that link)
101
What property of a form element object contains all of the form's controls.
elements property HTMLFormElement.elements returns an HTMLFormControlsCollection listing all the form controls contained in the form element you can obtain just the number of form controls using the length property you can access a particular form control in the returned collection by using either an index or the element's name or id (you should just use the name attribute!!)
102
What property of form a control object gets and sets its value?
value property
103
What is one risk of writing a lot of code without checking to see if it works so far?
it'll be hard to find your error(s) throughout the code
104
What is an advantage of having your console open when writing a JavaScript program?
the console will let you know if there's an error
105
What does HTMLFormElement.reset() method do?
this method restores a form element's default values it does the same thing as clicking the form's reset button
106
Why is it possible to listen for events on one element that actually happen its descendent elements?
this is due to event bubbling-- when you hover over a link, for example, javascript can trigger events on the a (anchor tag) element and also any elements the a (anchor tag) element sits inside
107
What DOM element property tells you what type of element it is?
event.target.tagName or event.target.nodeName
108
How can you remove an element from the DOM?
using the remove() method syntax: node.remove()
110
What does the element.closest() method take as its argument and what does it return?
takes a selector as an argument and returns itself or the cloest matching ancestor this method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor
110
What is the event.target?
it returns the node/element that was targeted by the function MDN: target property of the Event interface is a reference to the object onto which the event was dispatched MDN: Event interface represents an event which takes place in the DOM. An event can be triggered by te user's action e.g. clicking the mouse button
111
What is the affect of setting an element to display: none?
it turns off the display of an element so that it has no effect on the layout as if the element did not exist
112
What does the element.matches() method take as an argument and what does it return?
it takes a selector string and returns a boolean
113
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?
you add the event listener to its parent element so using event delegation
114
What is the event.target?
traget property of the event object holds the dom element that the user interact with to trigger that element element that interacted with (most directly) that trigger that event it doesn't matter where you put the event.target you put on, event target will only trigger the element that get interacted with
115
What is the affect of setting an element to display: none?
display: none, position: absolute and fixed -- the document act like these dont exist except for display nis being removed from the documne tlfow
116
What does the element.matches() method take as an argument and what does it return?
takes a selector and returns a boolean
117
How can you retrieve the value of an element's attribute?
getAttribute() method
118
At what steps of the solution would it be helpful to log things to the console?
afterafter every step
119
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 add an event listener to every tab
120
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?
you would have to write an if statement for each tab
121
What is JSON?
a text-based data format following on JS object syntax it's useful when you want to transmit data across a network
122
What are serialization and deserialization?
serialization is the process of turning an object in memory into a stream of bytes (turning object, array, value to json string?) vs deserialization is the reverse of serialization by turning a stream of bytes into an object in memory (turning json string back into object, value?)
123
Why are serialization and deserialization useful?
serialization is useful for sending data over the network deserialization is useful for recreating/retrieving the object in the same state as when you serialized it people often serialize objects in order to save them to storage, or to send as part of communications. Deserialization is the reverse of that process, taking data structured from some format, and rebuilding it into an object today, the most popular data format for serializing data is JSON.
124
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
125
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
126
How do you store data in the localStorage property?
setItem() method localStorage.setItem()
127
How do you retrieve data from the localStorage property?
getItem() method localStore.getItem()
128
What data type can localStorage save in the browser?
string
129
When does the 'beforeunload' event fire on the window object?
when the window, the document and its resources are about to be unloaded refreshing your page, closing browser, navigating to another link would trigger beforeunload
130
What is a method?
a method is a function which is a property of an object 2 kinds of methods: 1) Instance Methods -- built in tasks performed by an object instance 2) Static Methods -- tasks that are called directly on an object constructor
131
How can you tell the difference between a method definition and a method call?
a method definition is an function that is being defined in an object with the function code block vs a method call is an object function that is being called with ()
132
Describe method definition syntax (structure).
property name describing the following function follow by a colon then the function keyword with open and close parentheses (), with optional parameters inside of the (), follow by an open curly brace, and the function code block, then a closing curly brace
133
Describe method call syntax (structure).
object name follow by the object's property and a ()
134
How is a method different from any other function?
a method has to be declared inside of an object methods are just normal function a function in a property method can referenced the object they're inside of
135
What is the defining characteristic of Object-Oriented Programming?
OOP can contain both data (properties) and behaviors (methods) instead of having to pass each data that you would need as arguments, you would have all of this data inside the function and the function can access all of these data (where you behaviors is stored)
136
What are the four "principles" of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
137
What is "abstraction"?
according to MDN, abstraction is a way to reduce complexity and allow efficient design and implementation in complex software systems it hides the technical complexity of systems behind simpler APIs
138
What does API stand for?
Application Programming Interface | allows programs to interact with each other in an abstract way
139
What is the purpose of an API?
give programmers a way to interact with a system in a simplified, consistent fashion (which is abstraction)
140
What is "this" in JavaScript?
"this" is an implicit parameter of all JS functions by default, "this" will be the global "window" object syntax: the value of "this" can be recognized as "the object to the left of the dot" when the function is called (as a method) the value of "this" is determined at call time
141
What does it mean to say that this is an "implicit parameter"?
it 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 as a variable implicit parameter is a value within each function without having to declare it yourself
142
When is the value of this determined in a function; call time or definition time?
the value of "this" is determined at call time
143
How can you tell what the value of this will be for a particular function or method definition?
you can't
144
How can you tell what the value of this is for a particular function or method call?
the value of this can be recognized as "the object to the left of the dot" when the function is called (as a method)
145
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
146
What is a prototype in JavaScript?
a JS prototype is simply an object that contains properties and methods that can be used by other objects
147
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?
we use the method that are defined on the prototype object it's the __proto__ property attached to each object (object, array..) and the proto property has a list of methods if it doesn't find the method, it returns an error
148
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
look at its prototype
149
What does the new operator do?
let us create a version/ an instance to use of one type of thing IMPT: your constuctor function should never return a statement, "This" is already returned implicitly because we used a new operator
150
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property prototype property should only be used on constructor functions
151
What does the instanceof operator do?
it will check if the object on the right of the operator is a prototype of the object on the left of the operator
152
What is a "callback" function?
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 ex: function greeting(name) { alert('Hello ' + name); } ``` function processUserInput(callback) { var name = prompt('Please enter your name.'); callback(name); } ``` processUserInput(greeting); this is an example of a synchronous callback, as it is executed immediately.
153
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?
by using setTimeOut function it allows us to call another function and a time determined by us
154
How can you set up a function to be called repeatedly without using a loop?
setInternal function
155
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0s
156
What do setTimeout() and setInterval() return?
a timer id they both pull from the same timer id (so they wont overlap?)
157
What is AJAX?
(USE DEFINITION IN BOOK) a technique for loading data into part of a page without having to refresh the entire page allows you to request data from a server and load it without having to refresh the entire page the data is often sent in JSON format
158
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
159
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest object
160
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load event?
161
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
because XMLHttpRequest is an object, therefore, it has access to the AddEventListener() method? due to inheritance, they both have EventTarget.prototype in their prototype chains
162
Why do we use the XMLHttpRequest object?
we use this it to issue http requests in order to exchange data between the web site and a server
163
What is Array.prototype.filter useful for?
useful for filtering out the values you don't want in an array and store it in a new array
164
What is Array.prototype.map useful for?
useful for looping over every element in an array and returning a new array
165
What is Array.prototype.reduce useful for?
when you want an accumulation of/combined an array resulting in a single output value (and the return doesn't have to be an array) to take a list of things and collapse it down
166
What is "syntactic sugar"?
syntax that is designed to make things easier to read and write
167
What is the typeof an ES6 class?
function
168
Describe ES6 class syntax.
- class keyword - name of class in with the first letter being capitalized - a pair of curly braces (this is call the body of the class) - within it, a constructor function - some methods definition (constructor function is where you can initialize the properties of an instance)
169
What is "refactoring"?
the process of restructuring computer code without changing or adding to its external behavior and functionality key thing: behavior of the system doesn't change
170
In JavaScript, when is a function's scope determined; when it is called or when it is defined?
when it is defined
171
What allows JavaScript functions to "remember" values from their surroundings?
closures
172
✧ what is a closure?
combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment) closure gives you access to an outer function's scope from an inner function in js, closures are created every time a function is created, at function creation time