JavaScript Flashcards
What is the purpose of variables?
stores values and updates values, also gives value permanence (don’t have to keep declaring variable value)
How do you declare a variable?
var varname;
How do you initialize (assign a value to) a variable?
assignment operator (=)
What characters are allowed in variable names?
can start with: $, _, letters
can contain: $, _, letter, numbers
What does it mean to say that variable names are “case sensitive”?
javascript distinguishes between lower and upper cased letters
What is the purpose of a string?
pass around characters not to be interpreted as code (text content)
What is the purpose of a number?
math
What is the purpose of a boolean?
store true/false values, used for decision making (this or that)
What does the = operator mean in JavaScript?
putting a value into something
How do you update the value of a variable?
reassign variable value
variablename = newvalue
What is the difference between null and undefined?
both return/represent nothing/emptiness,
null: intentional nothingness (null is an assigned value), typically temporary space holder to be filled later
undefined typically unused by developers - usually a return from the console signifying the value has been deleted or was not assigned a value
Why is it a good habit to include “labels” when you log values to the browser console?
gives a point of reference
Give five examples of JavaScript primitives.
string, number, boolean, undefined, null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
adds strings/numbers together to one larger string
What purpose(s) does the + plus operator serve in JavaScript?
arithmetic operation and string concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the value and assigns the sum to the variable
What are objects used for?
area to store related properties
What are object properties?
variables within objects (/associated with objects)
Describe object literal notation.
curly braces + properties and values + object name
How do you remove a property from an object?
delete operator
What are the two ways to get or update the value of a property?
dot or bracket notation
object.property
object[‘property’]
bracket notation allows the text within to be interpreted by javascript (eg you can use variable names)
What are arrays used for?
ordered lists
Describe array literal notation.
square brackets [ ] with a comma between each item
How are arrays different from “plain” objects?
arrays are ordered, items are indexed (basically an object w keywords as consecutive numbers), arrays will repair themselves if items are deleted, arrays have a set length
What number represents the first index of an array?
0
What is the length property of an array?
calculate/count and stores the number of items in the array
How do you calculate the last index of an array?
subtract length by one
What is a function in JavaScript?
a repeatable series of statements to accomplish an operation
Describe the parts of a function definition.
function keyword
function name
parameters
code block
Describe the parts of a function call.
function name
parentheses
arguments if required
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call is much shorter since it doesn't have to define the function in curly braces function calls contain arguments in ' ' while definitions contain parameters
What is the difference between a parameter and an argument?
parameters are placeholders for arguments (which are real data)
What two effects does a return statement have on the behavior of a function?
allows us to assigns the result of the function
What two effects does a return statement have on the behavior of a function?
allows us to assigns the result of the function and ends the function
Why do we log things to the console?
keep track of js and verify output
What is a method?
function that is a property of an object
How is a method different from any other function?
methods are called using a dot notation bc they are a property of an object
How do you remove the last element from an array?
pop method
How do you round a number down to the nearest integer?
floor method of the Math object
How do you generate a random number?
random method of the Math object
How do you delete an element from an array?
splice method to remove at an arbitrary point, pop to remove at the end, shift to remove at the beginning
How do you append an element to an array?
push method
How do you break a string up into an array?
split method
Do string methods change the original string? How would you check if you weren’t sure?
no strings are immutable, console log the original string variable or look it up on mdn
Roughly how many string methods are there according to the MDN Web docs?
a lot (~30)
Is the return value of a function or method useful in every situation?
no, sometimes return is already given like a function(?)
Roughly how many array methods are there according to the MDN Web docs?
a lot (~30)
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.
==, !=, >, =
What data type do comparison expressions evaluate to?
booleans
What is the purpose of an if statement?
checks conditions
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword,
condition within parentheses,
code to be executed if true in curly braces
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
logical operators
(condition 1) && (condition 2)
What is the purpose of a loop?
repeat a function however needed
What is the purpose of a condition expression in a loop?
condition determines the start/stop of the loop
What does “iteration” mean in the context of loops?
a repeat of the loop code block
When does the condition expression of a while loop get evaluated?
beginning of the loop and before each iteration
When does the initialization expression for a for loop get evaluated?
once at the beginning of the loop
When does the condition expression for a while loop get evaluated?
before each iteration
When does the final expression of a for loop get evaluated?
at the end of each iteration
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?
increments the variable and reassigns incremented value to the variable and the end of the expression if placed at the end (i++), assigns at the before the end if placed before (++i)
How do you iterate through the keys of an object?
for in loop
Why are parameters useful?
reusability with different values
What is JSON?
“JavaScript Object Notation” text-based data format used to transmit data across a network
What are serialization and deserialization?
conversion of data to bytes and back in an organized way
Why are serialization and deserialization useful?
data storage and transfer
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?
localStorage.setItem takes key and value input
How to you retrieve data from localStorage?
localStorage.getItem takes key from local storage
What data type can localStorage save in the browser?
strings
When does the ‘beforeunload’ event fire on the window object?
when window/document are about to be unloaded
What is a method?
a function which is a property of an object (stored in an object)
How can you tell the difference between a method definition and a method call?
method definitions include a function definition whereas method calls only include object.method(parameters)
Describe method definition syntax (structure).
var variable = { methodName: function(parameters) { return statement }
Describe method call syntax (structure).
object.methodName(parameter)
How is a method different from any other function?
methods can only be called within the object
What is the defining characteristic of Object-Oriented Programming?
objects can contain data and behavior
data and behavior are paired
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
being able to work with complex things in simple ways
What does API stand for?
application programming interface
What is the purpose of an API?
give programmers a simplified way to interact with a system
What is ‘this’ in JavaScript?
an implicit parameter of all JavaScript functions, the object that the function is acting on
What does it mean to say that this is an “implicit parameter”
this is not explicitly defined
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); } };
‘this’ currently refers to nothing as it has not been called yet
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!’
when assigning value to var hello, value of character.greet is now free floating and no longer attached to the object, but instead attached to the window object where there is no firstName property
thus ‘this’ is being invoked on window and cannot access firstName
How can you tell what the value of this will be for a particular function or method definition?
you don’t know
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot
What kind of inheritance does the JavaScript programming language use?
prototypal inheritance
What is a prototype in JavaScript?
a template object which other objects can inherit methods and properties from
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?
prototypal inheritance
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
parent prototype in the __proto__ property
What does the new operator do?
creates a blank javascript object, adds proto property, links constructor prototype, ‘this’ now refers to new object, returns ‘this’ if function doesnt return an object
What property of JavaScript functions can store shared behavior for instances created with new?
function.prototype
What does the instanceof operator do?
test if the prototype property of a constructor appears anywhere in the prototype chain of an object
What is a “callback” function?
a function definition that is being passed around as a value/variable to other functions to be invoked later
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()
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?
interval ID as an integer
What is AJAX?
programming practice of building websites that can update information without reloading the entire page
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
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They share the same prototype property. The event target prototype
What is Array.prototype.filter useful for?
returning parts of arrays that satisfy a certain condition
What is Array.prototype.map useful for?
transforming an entire array and returning an array
What is Array.prototype.reduce useful for?
incrementing through an array without a for loop
What must the return value of myFunction be if the following expression is possible?
myFunction()();
another function
What does this code do? const wrap = value => () => value;
const wrap = function(value) { function() { return value; } }
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
function definition
What allows JavaScript functions to “remember” values from their surroundings?
the lexical environment, aka closure