JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Variable is a container for a value, like a number we might use in a sum, or a string that we might use as a part of a sentence.

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

How do you declare a variable?

A

We type the keyword (var, let, const) followed by the name you want to call your variable.

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

First type the variable name, followed by the equals sign(=), followed by the value you want to give it.

Example: var firstName = ‘Dylan’;

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 (cannot start with a number!) dollar sign($), or an underscore

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

Variable names care about the case sensitivity of their names, meaning a variable named score and Score would be different because of the capitalization of the letter S in score.

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 are pieces of text, they must be wrapped within quote marks.

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

calculating numbers, doing math stuff

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

True or False values, they are generally used to test a condition, after which code is run as appropriate.

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

1 equal sign is used for assignment of variables

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

Once the variable has been initialized, you can change (or update) that value by giving it a different 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 means empty can only be assigned, undefined means a variable is declared but has no actual arguments.

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

A label in the console.log is a simple way to describe the variable or value being logged.

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

Give five examples of JavaScript primitives:

A

string, number, boolean, null, and undefined

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

What data type is returned by an arithmetic operation?

A

single numerical value (number)

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

What is string concatenation?

A

the process of appending one string to the end of another string and so on.

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

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

A
  • addition in math

- string 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 ( true or false )

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

What does += “plus equals” operator do?

A

+= or “plus-equals” adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.

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

What are objects used for?

A

It is used to store various keyed collections and more complex entities

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

What are object properties?

A

Properties are the values associated with a JavaScript object.

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

Describe the object literal notation:

A

The Object literal notation is basically 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, just like a regular array. Values created with anonymous functions are methods of your object.

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

delete operator

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

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

A

dot notation and bracket notation

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

What are arrays used for?

A

Storing a collection of multiple items under a single variable name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Describe the array literal notation:
var arrayName = [ ]; separate indexes by commas
26
How are arrays different from "plain" objects?
Arrays have an order, stored within square brackets, have numeric indexes
27
What number represents the first index of an array?
0
28
What is the length property of an array?
returns the length of the array (counts each index and returns number value)
29
How do you calculate the last index of an array?
var variableName = array[array.length - 1] | subtract 1 from length of the array
30
Describe the parts of a function definition:
``` function (keyword) optionalName(parameters){ (code) return }; ```
31
Describe the parts of a function call
functionName(arguments,arguments); -arguments are optional
32
When comparing them side-by-side, what are the differences between a function call and a function definition?
33
What is the difference between a parameter and an argument?
34
Why are function parameters useful?
they provide placeholders when writing code
35
What two effects does a return statement have on the behavior of a function?
1) it exits the functions code block | 2) it gives back a value
36
Why do we log things to the console?
console.log is a debugging tool to check for errors
37
What is a method?
a method is a function which is a property of an object
38
How is a method different from any other function?
- methods are functions | - methods are attached to an object
39
How do you remove the last element from an array?
.pop()
40
How do you round a number down to the nearest integer?
math.floor()
41
How do you generate a random number?
math.random()
42
How do you delete an element from an array?
.splice()
43
How do you append an element to an array?
.push()
44
How do you break a string up into an array?
.split()
45
Do string methods change the original string? How would you check if you weren't sure?
They remain the same unless you assign it to a new variable. You can check by console.log()
46
Roughly how many string methods are there according to the MDN web docs?
around 30 string methods
47
Is the return value of a function or method useful in every situation?
NO
48
Roughly how many array methods are there according to MDN web docs?
around 25 array methods
49
What three-letter acronym should you always include in your Google search about a javaScript method or CSS property?
MDN
50
Give 6 examples of comparison operators:
``` Is Equal to (==) is not equal to (!=) Greater than (>) Less than (=) Less than or equal to (<=) ```
51
What data type do comparison expressions evaluate to?
Booleans (true or false)
52
What is the purpose of an IF statement?
If statement is a decision-making statement that guides a program to make decisions based on specified criteria.
53
is ELSE required in order to use an IF statement?
NO
54
Describe the syntax of an IF statement:
if (condition) { return}
55
What are the three logical operators?
``` Logical And (&&) Logical Or (||) Logical Not (!) ```
56
How do you compare two different expressions in the same condition?
Logical And (&&) or Logical OR (||)
57
What is the purpose of a loop?
A loop is a programming structure that repeats a sequence of instruction until a specific condition is met.
58
What is the purpose of a condition expression in a loop?
to check if the code needs to keep iterating
59
What does "iteration" mean in the context of loops?
repetition of the for loop code block
60
When does the condition expression of a while loop get evaluated?
before each iteration of the loop
61
When does the initialization expression of a for loop get evaluated?
BEFORE ANYTHING , ONLY HAPPENS ONE TIME, it happens in the beginning
62
When does the condition expression of a FOR loop get evaluated?
after initialization, before each iteration the condition is checked to allow the loop to get running until condition is met.
63
When does the final expression of a FOR loop get evaluated?
at the end of each iteration and before the condition is checked again.
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?
Adds one (value of variable goes up by 1 and assigns that to new variable) ex. var i = 0 i++ i now equals 1.
66
How do you iterate through the keys of an object?
FOR IN loop
67
Why do we log things to the console?
We use it to debug our code and log our progress
68
What is a 'model'?
Example of a structure, representation of an object
69
Which document is being referred to in the phrase Document Object Model?
HTML
70
What is the word 'object' referring to in the phrase Document Object Model?
JavaScript
71
What is a DOM Tree?
DOM tree is the model of the DOM, its a tree of elements with all of its children elements.
72
Give two examples of document methods that retrieve a single element from the DOM:
document.getElementByID(‘id’) & document.querySelector(‘css selector’)
73
Give one example of a document method that retrieves multiple elements from the DOM at once.
document. getElementsByClassName(‘class’) document. getElementsByTagName(‘tag’) document. querySelectorAll(‘css selector’)
74
Why might you want to assign the return value of a DOM query to a variable?
- It saves time for the browser looking through the DOM Tree to find the same element again - It stores the variable as a reference
75
What console method allows you to inspect the properties of a DOM element object?
console.dir(' ');
76
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
It allows all the html in the body to load first before the javascript loads, it can prevent errors and speed up website response time.
77
What does document.querySelector() take as its argument and what does it return?
‘Css’ selector’ and returns a single element node
78
What does document.querySelectorAll() take as its argument and what does it return?
‘Css selector’ and returns one or more elements as a NodeList.
79
What is the purpose of events and event handling?
- Event listeners wait for a specific event to occur and does a responsive function in response to it - Handling is creating code to create events (functions, has event parameter)
80
Are all possible parameters required to use a JavaScript method or function?
NO
81
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener()
82
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.
83
What object is passed into an event listener callback when the event fires?
Event
84
What is the event.target? If you weren't sure, how would you check? Where could have you get more information about it?
- It tells you were the event occurred on the page | - MDN
85
What is the difference between these two snippets of code?
- The second one has a callback function | - The second one has the handle click being called
86
What is the claaName property of element objects?
it gets and sets the value of a class attribute
87
How do you update the CSS class attribute of an element using JavaScript?
querySelector() first and assign to variable use the class name property to modify
88
How do you update text within an element using JavaScript?
update the text within the element
89
is the event parameter of an event listener callback always useful?
NO
90
Why is storing information about a program in a variable better than only storing it in the DOM?
much more efficient and easier
91
What event is fired when a user's cursor leaves a form control?
blur
92
What event is fired as a user changes the value of a form control?
input
93
What event is fired when a user clicks the "submit" button within a ?
submit
94
What event is fired when a user places their cursor in a form control?
focus
95
What does the event.preventDefault() method do?
prevents the browser from reloading the page
96
What does submitting a form without event.preventDefault() do?
reload the page
97
What property of a form element object contains all of the form's controls.
elements property
98
What property of a form control object gets and sets its value?
value property
99
What is one risk of writing a lot of code without checking to see if it works so far?
easy to get lost and difficult to retrace your steps, makes debugging harder
100
What is an advantage of having your console open when writing a JavaScript program?
check your progress, check for errors
101
Does the document.createElement() method insert a new element into the page?
No, you must use appendChild() method to add elements into a page
102
How do you add an element as a child to another element?
appendChild() method
103
What do you pass as the arguments to the elements.setAttribute() method?
('name', 'value)
104
What steps do you need to take in order to insert a new element into a page?
1. createElement | 2. appendChild
105
What is the textContent property of an element object do?
checks the text of an element or sets new text for it
106
Name two ways to set the class attribute of a DOM element?
setAttribute className
107
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Reusability, can use the function elsewhere in your code gives a name to a process
108
What is event.target?
Finds where the event was dispatched from
109
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event bubbling (the event starts at the most specific node and flows OUTWARD to the least specific one).
110
What DOM element property tells you what type of element it is?
-event.target.tagName() tagName is always all CAPS
111
What does the element.closest() method take as its argument and what does it return?
Takes a css selector as the argument, returns the event.target of the parent element where the event occured
112
How can you remove an element from the DOM?
remove() closestElement.remove()
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?
assign the event to the parent of the element
114
What is the event.target?
DOM element where the event occured
115
What is the affect of setting an element to display:none?
it makes the element invisible and removes it from the document flow
116
What does the element.matches() method take as an argument and what does it return?
takes a css.selector as an argument as a 'string' and it returns the element of which you called the element on
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?
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 to add an eventListener to every single element
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 check each element individually instead
121
What is JSON?
JavaScript Object Syntax, text-based format for representing structured data based on JavaScript Object Syntax. Used for transmitting data in Web Applications. Exists as a string - Useful when you want to transmit data across a network.
122
What is Serialization and Deserialization?
- Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network. - Deserialization is the reverse process: turning a stream of bytes into an object in memory.
123
Why are serialization and deserialization useful?
Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string
124
How do you serialize a data structure into a JSON string using JavaScript?
- JSON.Stringify | - Takes a JavaScript Object and then transforms it into a JSON string
125
How do you deserialize a JSON string into a data structure using JavaScript?
- JSON.parse | - Takes a JSON string and then transforms it into a JavaScript Object
126
How do you store data in LOCAL STORAGE?
SetItem()
127
How do you retrieve data from local storage?
getItem()
128
What data type can local storage save in the browser?
strings
129
When does the 'beforeunload' event fire on the window object?
Before the page unloads(close tab or refresh)
130
What is a method?
function that is a property of an object.
131
How can you tell the difference between a method definition and a method call?
1. call --> object.call()--> has paranthesis. | 2. definition --> funcction defintion + code block
132
Describe method definition syntax (structure).
have a property name: function keyword (parameters) { code block} object.property -->and then assign funcction
133
Describe method call syntax (structure).
object name.methodname();
134
How is a method different from any other function?
we need dot or bracket notation to show where that method is coming from. its insidde an object
135
What is the defining characteristic of Object-Oriented Programming?
Objects can container datas and behavior under the object
136
What are the four "principles" of Object-Oriented Programming?
1. abstraction 2. encapsulation 3. inheritance 4. polymorphism
137
What is "abstraction"?
Making complex things into simply ways.
138
What does API stand for?
Application programming interface
139
What is the purpose of an API?
allows users to use the interface independently of the implementation (abstraction). Allows users to work with the system in a simple way fashion
140
What is this in JavaScript?
Implicit parameter of every js function
141
What does it mean to say that this is an "implicit parameter"?
It is available in a function’s code black even though it was never included in the function's parameter list or declared with var
142
When is the value of this determined in a function; call time or definition time?
Call Time
143
What does this refer to in the following code snippet?
There is not definition because we are not in call yet, only in definition time referring to character object.
144
Given the above character object, what is the result of the following code snippet? Why?
The result will be the string value Its me mario. we are calling from character.
145
Given the above character object, what is the result of the following code snippet? Why?
“Its a me, undefined’ window doesn't have an object
146
How can you tell what the value of this will be for a particular function or method definition?
Nothing - can't tell what the value is
147
How can you tell what the value of this is for a particular function or method call?
Object left to the dot. If there is no dot, then it's the window.
148
What kind of inheritance does the JavaScript programming language use?
Prototype-based Inheritance
149
What is a prototype in JavaScript?
​​Prototypes are the mechanism by which JavaScript objects inherit features from one another
150
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?
Prototype-based Inheritance
151
If an object does not have it's own property or method by a given key, where does JavaScript look for it?
Prototype
152
What does the new operator do?
It creates an instance of a user-defined object type or of one of the built-in objects types that has a constructor function. Creates blank javascript object Adds a property to the new object (__proto__) that links to the constructor function's prototype object Give this variable a value Return this if the function doesn’t retur
153
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype
154
What does the instanceof operator do?
Tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
155
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.
156
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?
clearInterval()
157
How can you set up a function to be called repeatedly without using a loop?
setInterval()
158
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
159
What do setTimeout() and setInterval() return?
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 clearInterval() to cancel the interval.
160
What is a client?
Service requesters
161
What is a server?
provider of the service
162
WhaWhich HTTP method does a browser issue to a web server when you visit a URL?
GET
163
What three things are on the start-line of an HTTP request message?
HTTP method; a verb or noun that describes action being performed The target request: either a URL or absolute path of the protocol, port, and domain are usually characterized by the request context. HTTP version that is being used
164
What three things are on the start-line of an HTTP response message?
Protocol version: http version Status code: example 404 Status text: text description of the status code to help humans understand http message
165
What are HTTP headers?
What are HTTP headers? | Providing more information about what we are doing. What content type, length of the content.
166
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
167
Is a body required for a valid HTTP request or response message?
No, They are not required!
168
What is AJAX?
Ajax, which initially stood for Asynchronous JavaScript And XML, is a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest.
169
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
170
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequests()
171
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
172
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
The both share prototypes