JS Flashcards
How do you declare a variable?
var, let, or const then name
How do you initialize (assign a value to) a variable?
equal ( = ) sign
What characters are allowed in variable names?
letters, numbers, symbols
What does it mean to say that variable names are “case sensitive”?
“name” is different from “Name”
What is the purpose of a string?
text content
What is the purpose of a number?
math stuff
What is the purpose of a boolean?
determine true or false
What does the = operator mean in JavaScript?
is assigned to
How do you update the value of a variable?
variable name = ‘new value’
What is the difference between null and undefined?
null - placed there on purpose, doesn’t exist
undefined - there’s nothing assigned to a variable
Why is it a good habit to include “labels” when you log values to the browser console?
to keep track of what the logs are for
Give five examples of JavaScript primitives.
string, number, boolean, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
putting two strings together
What purpose(s) does the + plus operator serve in JavaScript?
addition or concatenating strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adding something to the variable, then updating that variable
What does the += “plus-equals” operator do?
adding something to the variable, then updating that variable
What are objects used for?
key, value lists
What are object properties?
variables specifically for objects
Describe object literal notation.
variable name, the curly-braces surrounding key-value pairs, separated by commas
How do you remove a property from an object?
delete operator then obj.key
What are the two ways to get or update the value of a property?
dot notation, or bracket notation
What are arrays used for?
lists
Describe array literal notation.
variable name, square bracket, then values separated by commas
How are arrays different from “plain” objects?
arrays’ keys are their index numbers
What number represents the first index of an array?
0
What is the length property of an array?
get how many items are in the array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
a set of statements that performs a task
Describe the parts of a function definition.
function keyword, optional function name, optional parameters (separated by a comma), function body, optional return statement
Describe the parts of a function call.
function name with parenthesis with optional arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call - function name and parenthesis (optional arguments) function definition - has all the jazz, tells the computer what to do
What is the difference between a parameter and an argument?
parameter - used in function definition
argument - used in function call
Why are function parameters useful?
can use keyword for a different things, instead of specifying the item again and again
What two effects does a return statement have on the behavior of a function?
- function produces a value that can be used
2. prevents more code in the block from running
Why do we log things to the console?
to inform developers what the code is doing
What is a method?
a function in an object
How is a method different from any other function?
it’s specifically attached to an object
How do you remove the last element from an array?
.pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array?
.splice(startIndex, # of items to remove)
How do you append an element to an array?
.push()
How do you break a string up into an array?
.split()
Do string methods change the original string? How would you check if you weren’t sure?
no, strings are immutable
use console.log(string) to check
Roughly how many string methods are there according to the MDN Web docs?
30+
Is the return value of a function or method useful in every situation?
no
Roughly how many array methods are there according to the MDN Web docs?
38+
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
===, !==, less than, greater than, less than or equal to, greater than or equal to
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
check if the statement is truthy then it runs a block of code, if not, it does something else
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
‘if’ keyword (statements) {
code block to run
}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
if (something || something else)
What is the purpose of a loop?
to repeat a block of code until a condition is met
What is the purpose of a condition expression in a loop?
to let code know when to start and stop
What does “iteration” mean in the context of loops?
a run through of a code (one loop)
When does the condition expression of a while loop get evaluated?
before the start run of every loop
When does the initialization expression of a for loop get evaluated?
once, at the very beginning
When does the condition expression of a for loop get evaluated?
before each loop runs
When does the final expression of a for loop get evaluated?
after each loop runs
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
adds 1 to the variable
How do you iterate through the keys of an object?
for (var key in obj)
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a form?
submit
What does the event.preventDefault() method do?
prevents browser from reloading the page with the form’s values in the URL
What does submitting a form without event.preventDefault() do?
reloads the page
What property of a form element object contains all of the form’s controls.
elements
What property of form a control object gets and sets its value?
.value
What is one risk of writing a lot of code without checking to see if it works so far?
hard to debug, would have to figure out which part isn’t working
What is an advantage of having your console open when writing a JavaScript program?
you can see if the code is working properly immediately
What is the event.target?
whatever element you applied the listener to that you focused/clicked on
The DOM element that you directly interacted with, that triggered the event
What is the effect of setting an element to display: none?
hidding the element
What does the element.matches() method take as an argument and what does it return?
argument - selector
returns a boolean
How can you retrieve the value of an element’s attribute?
.getAttribute
At what steps of the solution would it be helpful to log things to the console?
every step!
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?
create individual event listeners
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?
using hard coding
What is JSON?
a text-based data format following JavaScript object syntax;
JSON exists as a string — useful when you want to transmit data across a network
What are serialization and deserialization?
Serialization - process where an object or data structure is translated into a format suitable for transferral over a network, or storage
Deserialization - the reverse process: turning a stream of bytes into an object in memory
Why are serialization and deserialization useful?
serialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. And deserialization allows us to reverse the process, which means reconverting the serialized byte stream to an object again.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
How to you store data in localStorage?
.setItem()
How to you retrieve data from localStorage?
.getItem()
What data type can localStorage save in the browser?
string
When does the ‘beforeunload’ event fire on the window object?
reloading page, clicking to different page, closing tab / browser
What is a method?
a function which is a property of an object
How can you tell the difference between a method definition and a method call?
method definition - function is saved as a property
method call - name of object. method name ()
Describe method definition syntax (structure).
method name: function
Describe method call syntax (structure).
objectName.methodName()
How is a method different from any other function?
nothing really;
a method is attached to an object and can only be called through the object
What is “abstraction”?
being able to work with (possibly) complex things in simple ways
What does API stand for?
application programming interface
What is the purpose of an API?
give programmers a way to interact with a system in a simplified, consistent fashion
What is the purpose of an API?
give programmers a way to interact with a system in a simplified, consistent fashion
What is this in JavaScript?
the object to the left of the dot at call time
When is the value of this determined in a function; call time or definition time?
call time
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); } };
nothing. this is not being called
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
“It’s-a-me, Mario!”
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!”
the run time at hello has nothing at the left of the dot
How can you tell what the value of this will be for a particular function or method definition?
this will be the object the method is a property of
How can you tell what the value of this is for a particular function or method call?
unkown
How can you tell what the value of this is for a particular function or method call?
object on the left side of the dot
What does it mean to say that this is an “implicit parameter”?
not explicitly defined, this is a value that changes in every function call
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance
What is a prototype in JavaScript?
an object that contains properties and (predominantly) methods that can be used by other objects
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?
they borrow methods from the prototype object
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
__proto__ property
What does the new operator do?
- Creates a blank, plain JavaScript object.
- Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
- Binds the newly created object instance as the this context
- Returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
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;
returns a boolean value
What is a “callback” function?
a function passed into another function as an argument
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()
setInterval()
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
timerID
What is AJAX?
a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest;
allows you to update parts of the DOM of an HTML page instead without the need for a full page refresh
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest object
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
inheritance
What is Array.prototype.filter useful for?
when you want a new array with only elements that passes a specific test
What is Array.prototype.map useful for?
when you want a new array with modified elements
What is Array.prototype.reduce useful for?
when you want a single output from a chunk of data