JavaScript Flashcards

1
Q

What is the purpose of variables

A

Used to store (remember) data; short-term memory

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 identifier (name)

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

Numbers and letters, $, and underscores; numbers cannot be the first character

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

Casing all needs to be the same or they are not the same

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

What is the purpose of a string

A

Store and manipulate text

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

What is the purpose of a number

A

To contain numeric data for calculations

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

What is the purpose of a Boolean

A

Represents true or false (binary values) useful for decision making

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

What does the = operator mean in JavaScript

A

Assignment operator; assigns a value to a variable

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

How do you update the value of a variable

A

Put the variable name again and using the assignment operator you just change the value

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

What is the difference between null and undefined

A

Null: represents nothing, something that doesn’t exist (intentional nothing put by the programmer)

Undefined: something isn’t there

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

Why is it good habit to include “labels” when you log values to the browser console

A

It’s more clear to see what data is being logged to the console and is a helpful debugging tool

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

Give five examples of JavaScript primitives

A

String, number, Boolean, undefined, null

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

What is string concatenation

A

Two or more strings joined together to make a single string

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

Addition or concatenation

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

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

Addition assignment; adds a value then updates the variable

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

What are objects used for

A

Used to group together sets of variables and functions that have some relation to each other (can include functionality) to model or represent something

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

What are object properties

A

They are variables used within an object

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

Describe object literal notation

A

Curtly braces: sets of key-value pairs (property or method and it’s value separated by a colon) and the key-value pairs which are separated by commas except for the last one

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

How do you remove a property from an object

A

Delete keyword followed by name of the object, member operator (.), and name of property or method you want to delete

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

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

A

Dot notation: object.property/method

Bracket notation: object[property/method]

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

What are arrays used for

A

Storing lists of values related to each other

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

Describe array literal notation

A

Data separated by commas within square brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How are arrays different from plain objects
Arrays have an order and their values are paired with their index (numeric index) Objects have set properties (alpha-numeric indices) and are just collections of data with no set order
26
What number represents the first index of an array
0
27
What is the length property of an array
Array.length Amount of items in an array
28
How do you calculate the last index of an array
Array.length - 1
29
What is a function in Javascript
A repeatable block of code designed to perform a specific task when it is invoked
30
Describe the parts of a function definition
Function keyword, optional function name, comma-separated parameter list of 0 or more parameters, open curly brace for the code block, optional return statement (return keyword followed by optional value), closing curly brace
31
When comparing them side-by-side, what are the differences between a function call and a function definition
The function call does not have the function keyword or code block within the curly braces
32
What is the difference between a parameter and an argument
Parameters are placeholders are variables that we declare in the function definition and that have an unknown value until we call the function and pass in the argument(a) Arguments are actual values that we pass in when calling the function
33
Why are function parameters useful?
Allows the function to be reusable
34
Why do we log things to the console?
to see what our code is doing, if our code is functioning properly, debugging tool
35
What is a method?
a function which is a property of an object
36
How is a method different from any other function?
methods are attached to objects; you need to say specifically where the method is coming from
37
How do you remove the last element from an array?
.pop() method
38
How do you round a number down to the nearest integer?
Math.floor()
39
How do you generate a random number?
Math.random() | gives you a decimal between 0 and 1 not including 1
40
How do you delete an element from an array?
.splice(index, #to delete, optional replacement)
41
How do you append an element to an array?
.push() and .unshift()
42
How do you break a string up into an array?
.split(separator)
43
Do string methods change the original string? How would you check if you weren't sure?
no, because strings are immutable, but you can use the console log to check
44
Roughly how many string methods are there according to the MDN Web docs?
~35
45
Is the return value of a function or method useful in every situation?
no
46
Roughly how many array methods are there according to the MDN Web docs?
~37
47
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
48
Give 6 examples of comparison operators.
> < == === >= <= !== !===
49
What data type do comparison expressions evaluate to?
booleans
50
What is the purpose of an if statement?
to make decisions, to tell the code what to do in certain situations
51
Is else required in order to use an if statement?
nope
52
Describe the syntax (structure) of an if statement.
if (condition) { code block; }
53
What are the three logical operators?
&& (and), || (or), ! (not, bang)
54
How do you compare two different expressions in the same condition?
using a logical operator (&& or ||) between the two expressions
55
What is the purpose of a loop?
repeatable code
56
What is the purpose of a condition expression in a loop?
to drive the loop to terminate
57
What does "iteration" mean in the context of loops?
each individual time the code block runs
58
When does the condition expression of a while loop get evaluated?
before each iteration
59
When does the initialization expression of a for loop get evaluated?
BEFORE ANYTHING; evaluated once
60
When does the condition expression of a for loop get evaluated?
after the initialization the first time and then happens every time BEFORE EACH
61
When does the final expression of a for loop get evaluated?
the very end of the loop, after the statement is executed | AFTER EACH
62
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
63
What does the ++ increment operator do?
increments by 1, changes the value of the variable
64
What event is fired when a user places their cursor in a form control?
focus
65
What event is fired when a user's cursor leaves a form control?
blur
66
What event is fired as a user changes the value of a form control?
input
67
What does the event.preventDefault() method do?
it prevents the page from reloading when the form is submitted
68
What does the event.preventDefault() method do?
it prevents the default (if there is one) from happening
69
What does submitting a form without event.preventDefault() do?
it will reload the page and the data will be attached to the url (default of form is to send the information of that form to the url to store)
70
What property of form a control object gets and sets its value?
value
71
What property of a form control object gets and sets its value?
value
72
What is an advantage of having your console open when writing a JavaScript program?
you can see what is happening as you work
73
What is an advantage of having your console open when writing a JavaScript program?
you can see what is happening as you work
74
Does the document.createElement() method insert a new element into the page?
no, we are just creating the element
75
How do you add an element as a child to another element?
parent.appendChild(child)
76
What do you pass as the arguments to the element.setAttribute() method?
name, value
77
What steps do you need to take in order to insert a new element into the page?
createElement, add any additional info to it, append it to the parent (assuming that parent is already part of the page)
78
What is the textContent property of an element object for?
to get, set, or update text withing that textNode
79
Name two ways to set the class attribute of a DOM element.
setAttribute className classList (be consistent)
80
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
1. allows it to be repeatable 2. you can give a function a name that makes it easily understandable when reading code 3. more efficient
81
Give two examples of media features that you can query in an @media rule.
aspect ratio | height (of viewport)
82
What is the event.target?
it's the element that the event is firing on; holds the dom element that you most directly interacted with
83
What is the effect of setting an element to display: none?
the element won't appear on the page; acts as if the element doesn't exist (removes the element from the document flow)
84
What does the element.matches( ) method take as an argument and what does it return?
argument: a string of the css selector you want to select return: boolean true or false
85
How can you retrieve the value of an element's attribute?
getAttribute
86
At what steps of the solution would it be helpful to log things to the console?
after every step
87
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 put an event listener on every single tab
88
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?
if statement for each tag
89
What is JSON?
(JavaScript Object Notation) | a text-based data format following JavaScript object syntax and it can be used independently from JS
90
What are serialization and deserialization?
serialization: converting a native object to a string so it can be transmitted across the network deserialization: Converting a string to a native object
91
Why are serialization and deserialization useful?
for data exchange and storing data without having to rely on it storing as a reference in your computer it allows you to save the state of an object and convert it to 1s and 0s so it can be transferred over networks and then it can be translated back into an object through deserialization
92
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( ) method
93
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( ) method
94
How to you store data in localStorage?
.setItem of the localStorage item allows us to store in localStorage
95
How to you retrieve data from localStorage?
.getItem
96
What data type can localStorage save in the browser?
string values
97
When does the 'beforeunload' event fire on the window object?
happens before the page will no longer be loaded (closing a tab, closing a browser window, navigating to a new window)
98
What is a method?
a function which is a property of an object
99
How can you tell the difference between a method definition and a method call?
method definition has a code block and function keyword whereas method call doesn't have the code block and you would pass in arguments if necessary
100
Describe method definition syntax (structure).
has a function keyword, optional function name followed by a comma-separated list of 0 or more parameters as well as curly braces for the code block
101
Describe method call syntax (structure).
the object name, member operator, the name of the function followed by a comma-separated list of 0 or more arguments in parentheses
102
How is a method different from any other function?
methods must be attached to an object, you must specify where that method is coming from ** you can reference data within the function
103
What is this in JavaScript?
"the object to the left of the dot" when the function is called (as a method). 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 ** if you cannot see the function being called, then you do not know what the value of this will be
104
What does it mean to say that this is an "implicit parameter"?
it is available in a function's code block even though it was never included in the function's parameter list or declared with var.
105
When is the value of this determined in a function; call time or definition time?
call time
106
What does 'this' refer to in the following code snippet? ``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ```
don't know until call time
107
Given the above character object, what is the result of the following code snippet? Why? character.greet();
"it's-a-me, Mario!" why:
108
What is this in JavaScript?
A keyword that refers to the object it belongs to ("the object to the left of the dot" when the function is called (as a method). 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 ** if you cannot see the function being called, then you do not know what the value of this will be)
109
How can you tell what the value of this will be for a particular function or method definition?
you can't tell until the method or function is called
110
How can you tell what the value of this is for a particular function or method call?
look at the object left of the dot when the function or method is being called
111
What does 'this' refer to in the following code snippet? ``` var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; ```
its nothing because there is nothing using this
112
Given the above character object, what is the result of the following code snippet? Why? character.greet();
"it's-a-me, Mario!" why: because this is referring to the character object and getting the firstName property out of that object which is "Mario"; we know it's character because it's left to the dot
113
Given the above character object, what is the result of the following code snippet? Why? ``` var hello = character.greet; hello(); ```
"it's-a-me, undefined!" why: there's no reference to the value of the variable this. if there's no dot at call time, the default is "window" you're no longer calling that function on the object that includes the this keyword so it's undefined
114
How can you tell what the value of this is for a particular function or method call?
look at the object left of the dot when the function or method is being called
115
What kind of inheritance does the JavaScript programming language use?
prototypal inheritance
116
What is a prototype in JavaScript?
an object that contains properties and (predominantly) methods that can be used by other objects
117
How is it possible to call methods on strings, arrays, and numbers even though those methods don't actually exist on objects, arrays, and numbers?
because of prototypal inheritance, they borrow the prototype object and its methods
118
How is it possible to call methods on strings, arrays, and numbers even though those methods don't actually exist on objects, arrays, and numbers?
because of prototypal inheritance, they borrow the prototype object and its methods *using methods on the prototype object
119
What does the new operator do?
1. creates a blank plain javascript object 2. adds a property to the new object that links it to the prototype 3. binds the new object instance as the "this" context 4. returns this if the function doesn't return an object
120
What does the new operator do?
creates an instance (1 iteration) of a type of thing 1. creates a blank plain javascript object 2. adds a property to the new object that links it to the prototype 3. binds the new object instance as the "this" context 4. returns this if the function doesn't return an object
121
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
122
What does the instanceof operator do?
checks if the object on the right of the operator is a prototype anywhere in the chain tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object returns a boolean value
123
What is a "callback" function?
a function passed into another function as data (argument) which is then invoked inside the outer function to perform a specific task
124
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?
setTimeout( ) function
125
How can you set up a function to be called repeatedly without using a loop?
setInterval( ) function
126
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
127
What do setTimeout() and setInterval() return?
timerId | the delay time, a positive integer value identifying the timer created by the call to setTimeout( ) or setInterval( )
128
What is AJAX?
Ajax allows you to request data from a server and load it without having to refresh the entire page
129
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
130
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest object
131
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load events
132
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
inheritance possibly?