JavaScript Flashcards
what is the purpose of variables?
store data for reuse
how do you declare a variable?
var
how do you initialize (assign a value to) a variable?
var x = ‘string’;
what characters are allowed in variable names?
letters, numbers, $, _
however must start with letter, $, or _.
must not use - or .
what does it mean to say that variable names are “case sensitive”
the slightest capitalization difference for the same word will count as different variables
what is the purpose of string?
used when working with any kind of text
what is the purpose of a number?
for tasks that involve counting or calculating sums
what is the purpose of boolean?
helpful when determining which part of a script should run/ acts as an on or off switch
what does the = operator mean in javascript?
assignment operator
how do you update the value of a variable?
use the variable name, the equals sign, and the new value
what is the difference between null and undefined?
null represents a reference that points to a nonexistent or invalid object or address
undefined is a value assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments
why is it a good habit to include labels when you log values to the browser console?
easier to keep track of console output for yourself and others
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?
placing string together to form a single output of string
what purpose(s) does the + plus operator serve in JS?
concatenation and addition
what data type is returned by comparing two values (,===, etc)?
boolean
what does the += plus-equals operator do?
adds the value of the right operand to a variable and assigns the result to the variable
what are objects used for?
objects group together a set of variables and functions to create a model of something you would recognize from the real world
what are object properties?
exclusive variables - another way to store data
describe object literal notation
assigning the object properties within { }
how do you remove a property from an object?
delete object.property
what are the two ways to get or update the value of a property
use dot or bracket notation
what are arrays used for?
when you are working with a list or a set of values that are related to each other
describe array literal notation
var colors = [ ] ;
how are arrays different from “plain” objects?
arrays use indexes to keep track of items
what number represents the first index of an array?
0
what is the length property of an array?
tells 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?
set of statements that perform a task
describe the parts of a function definition
function keyword, optional name, parameters, code block, return statement, {}
describe the parts of a function call
function name, arguments ( )
when comparing them side-by-side, what are the differences between a function call and a function definition?
function call - actually using the function
function definition - creating the code to run
what is the difference between a parameter and an argument?
parameters are variables that doesn’t have a value yet
arguments are the user values
why are function parameters useful?
used as placeholders for when the function is called
what two effects does a return statement have on the behavior of a function?
causes the function to produce a value we can use
prevents any more code in the function’s code block from being run
why do we log things to the console?
during development, check if output matches what you are expecting
debugging
what is a method?
function which is a property of 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()
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?
they return a new string
check with MDN - documentation or try yourself on console log
roughly how many string methods are there according to the MDN web docs?
~35 remember to check MDN
is the return value of a function or method useful in every situation?
no, sometimes function/methods do not need to return a value
roughly how many array methods are there according to the MDN web docs?
~30 remember to check MDN
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?
boolean
what is the purpose of an if statement?
checks if a condition is true or false to execute code
is else required in order to use an if statement?
no, can be used to just check condition
describe the syntax(structure) of an if statement
if conditional statement, ( condition ), { } if code block
what are the three logical operators
&& logical and
|| logical or
! logical not
how do you compare two different expressions in the same condition?
logical and, logical or
what is the purpose of a loop?
loops check a condition; if it returns true, a code block will run. condition checked again and will repeat if still true. repeats until condition returns false
what is the purpose of a condition expression in a loop?
have the loop continue to run until the counter reaches a specified number
what does “iteration” mean in the context of loops?
iteration refers to the process in which the content of loop is executed once
when does the condition expression of a while loop get evaluated?
evaluated before each code block
when does the initialization expression of a for loop get evaluated
evaluated before the loop begins
when does the condition expression of a for loop get evaluated
after initialization, before code block runs
when does the final expression of a for loop get evaluated?
evaluated at the end of each loop iteration. this occurs before the next evaluation of a condition
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 one
how do you iterate through the keys of an object?
for/in loop
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?
tells the user agent that if the vent does not get explicitly handled, its default action should not be taken as it normally would be
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 property
what is one risk of writing a lot of code without checking to see if it works so far?
there could be some typo or miscalculated result that messes with the whole code
what is an advantage of having your console open when writing a javascript program?
you can test things out and prevent possible bugs
what is the event.target?
reference to the object onto which the event fired
what is the effect of setting an element to display: none?
element will not be visible
what does the element.matches() method take as an arugment and what does it return?
selectorString as argument
returns boolean
how can you retrieve the value of an element’s attribute?
.getAttribute() method
at what steps of the solution would it be helpful to log things to the console?
whenever you are using a method, property, or calculation
if you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JS code be writen instead?
need to add event listeners to each new node
if you didn’t use a loop to conditionally show or hide the views in the page, how would your JS code be written instead?
you would have to get the variable for each index and compare for every single one
what is JSON?
text-based data format following JS object syntax
what are serialization and deserialization?
serialization - process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage
deserialization - fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state
why are serialization and deserialization useful?
compress/decompress data to keep accurate data when sharing with others
how do you serialize a data structure into a JSON string using Javascript?
JSON.stringify() method
how do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse() method
how do you store data in localStorage?
.setItem() method
how do you retrieve data from localStorage?
.getItem() method
what data type can localStorage save in the browser?
string
when does the ‘beforeunload’ event fire on the window object?
fired when the window, the document, and its resources are about to be unloaded (when user leaves/navigates away from the page
what is a method?
function which is a property of an object
how can you tell the difference between a method definition and a method call?
method definition is where the method is built with code block
method call is when the method definition is being used
describe method definition syntax (structure)
variable object declration followed by property name and function() with code block
describe method call syntax(structure)
method.function();
how is a method different from any other function?
a method is a function, which is a property of an object. use dot notation/bracket notation to access
what is the defining characteristic of object-oriented programming?
objects can contain both data (as properties) and behavior (as methods)
what are the four “principles” of object-oriented programming
abstraction
encapsulation
inheritance
polymorphism
what is abstraction?
the process of removing physical, spacial, or temporal details or attributes in the study of objects or system to focus attention on details of greater importance; similiar in nature to the process of generalization
ex; flicking the lights is more than just turning a switch. there are many micro events that occur, but all you need to know is that you need to flip the switch to turn on/off the lights
what does API stand for?
application programming interface
what is the purpose of an API?
it defines interactions between multiple software intermediaries. it defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conversations to follow, etc.
what is this in JS?
the object
a property of an execution context(global, function, or eval) that, in non-strict mode is always a reference to an object and in strict mode can be any value
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
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); } };
we don’t know yet as this is the definition time
given the above character object, what is the result of the following code snippet? why?
character.greet();
It’s a me, Mario!
greet is a function that will return a message
given the character object, what is the result of the following code snippet? why?
var hello = character.greet;
hello();
it’s a me, undefined
because this is being used on the window object which doesn’t have a firstName property defined
how can you tell what the value of this will be for a particular function or method definition?
not in strict mode, this will default to the global object, which is a window in a browser
in strict mode, if the value of this is not set when entering an execution context, it remains as undefined
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)
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
what kind of inheritance does the JS programming language use?
prototype-based inheritance
a style of object-oriented programming in which behavior reuse (known as inheritance) is performed via a process of reusing existing objects that serve as prototypes
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?
prototype chain
if an object does not have it’s own property or method by a given key, where does JS look for it?
prototype object, then to the object’s
what does the new operator do?
lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function
- creates a blank, plain JS object
- links the newly created object to another object by setting the other object as its parent prototype
- passes the newly created object from step 1 as this context
- returns this if the function doesn’t return an object
what property of JS functions can store shared behavior for instances created with new?
prototype property
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. return value is a boolean value
what is a callback function?
function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action
besides adding an event listener callback function to an element or the document, what is one way to delay the execution of JS function until some point in the future?
setTimeout() method
how can you set up a function to be called repeatedly without using a loop?
setInterval() method
what is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
value of 0 is used, meaning execute “immediately”, or more accurately, the next event cycle
what do setTimeout() and setInterval() return?
setTimeout() return is a positive integer value which identifies the timer created by the call to setTimeout()
setInterval() return is a numeric, non-zero value which identifies the timer created by the call to setInterval()
what is AJAX?
a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequeseet
what does the AJAX acronym stand for?
asynchronous Javascript and XML
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?
shares prototype
what is Array.prototype.filter useful for?
creates a new array with all elements that pass the test implemented by the provided function
what is Array.prototype.map useful for?
it creates a new array populated with results of calling a provided function on every element in the calling array
what is Array.prototype.reduce useful for?
when you want to reduce each element of an array to a single output value