LFZ JS Quiz Flashcards
What is the purpose of variables?
To store information
How do you declare a variable?
with the var keyword
How do you initialize (assign a value to) a variable?
with the assignment operator =
What characters are allowed in variable names?
$, _
What does it mean to say that variable names are “case sensitive”?
Capitalization matters
What is the purpose of a string?
to store an array of characters
What is the purpose of a number?
to store a number or float
What is the purpose of a boolean?
to store true or false
What does the = operator mean in JavaScript?
it is the assignment operator
How do you update the value of a variable?
assign the variable to another value with the assignment operator
What is the difference between null and undefined?
null is an empty object while undefined is nothing
Why is it a good habit to include “labels” when you log values to the browser console?
For clarity in debugging
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?
combining two stings together into one
What purpose(s) does the + plus operator serve in JavaScript?
to add numbers or concatenate strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the result with itself
What are objects used for?
To group together a set of variables and functions
What are object properties?
the object’s variables
Describe object literal notation.
assign a var to an object by putting the object inside curly braces
How do you remove a property from an object?
using the delete keyword
What are the two ways to get or update the value of a property?
using dot notation or bracket notation
What are arrays used for?
To store data, mainly lists
Describe array literal notation.
var foo = []
How are arrays different from “plain” objects?
Stores data with index value as the keys
What number represents the first index of an array?
0
What is the length property of an array?
the amount of values it has
How do you calculate the last index of an array?
subtract 1 from the length method of the array object
What is a function in JavaScript?
A group of code that can be reused and called with arguments
Describe the parts of a function definition.
the name, the parameters, the code
Describe the parts of a function call.
the name, the arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function call just needs the function name and arguments. A function definition uses the function keyword and has code.
What is the difference between a parameter and an argument?
a parameter is like a temporary variable that takes in input, while the arguments are the input
Why are function parameters useful?
Pass data into the function
What two effects does a return statement have on the behavior of a function?
the return statement breaks out of the function and returns the value
Why do we log things to the console?
For debugging
What is a method?
A function that is the property of an object
How is a method different from any other function?
It is apart of an object and is called using dot notation or bracket notation
How do you remove the last element from an array?
using the pop method of the array object
How do you generate a random number?
using the random method of the Math object
How do you delete an element from an array?
using the splice method of the array object
How do you append an element to an array?
using the push method of the array object
How do you break a string up into an array?
using the split method of the array object
Do string methods change the original string? How would you check if you weren’t sure?
No they do not, you can check the documentation
Roughly how many string methods are there according to the MDN Web docs?
~50
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?
~50
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.
, <=, >=, ==, ===
Give 6 examples of comparison operators.
, <=, >=, !==, ===
What data type do comparison expressions evaluate to?
Boolean
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, expression, and codeblock
What are the three logical operators?
&&, ||,
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
using logical operators
What is the purpose of a loop?
To repeat something code a number of times
What is the purpose of a condition expression in a loop?
To determine if the loop should start, run, and end
What does “iteration” mean in the context of loops?
the amount of loops are run
When does the condition expression of a while loop get evaluated?
at the start of the loop iteration
When does the initialization expression of a for loop get evaluated?
before the loop is run
When does the condition expression of a for loop get evaluated?
before each loop iteration
When does the final expression of a for loop get evaluated?
at the end of the loop 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?
it is short hand for adding 1 to itself
How do you iterate through the keys of an object?
by using the 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 ?
submit
What does the event.preventDefault() method do?
prevents the events default action to be run
What does submitting a form without event.preventDefault() do?
resets 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 an advantage of having your console open when writing a JavaScript program?
it’s easier to debug and you can see any errors
How do you add an element as a child to another element?
using the addChild method
Does the document.createElement() method insert a new element into the page?
no
How do you add an element as a child to another element?
using the appendChild method or append method
What do you pass as the arguments to the element.setAttribute() method?
element attribute names and value
What steps do you need to take in order to insert a new element into the page?
create it, set the properties, and append it
What is the textContent property of an element object for?
it holds the text content of the element
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Reusability and flexibility
What is the event.target?
the target node of the point of interaction
What is the affect of setting an element to display: none?
it removes it from the document flow and hides it
What does the element.matches() method take as an argument and what does it return?
it takes in a css selector and returns a boolean
How can you retrieve the value of an element’s attribute?
by using the getAttribute method
At what steps of the solution would it be helpful to log things to the console?
at pretty much every point
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 another event listener to that new tab
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 have a lot of conditionals and over all use very messy code
What is JSON?
A text-based data format that follows javacript’s object syntax
What are serialization and deserialization?
Turning an object in memory into a stream of bytes to work with. Deserialization is the reverse; turning a stream of data into an object
Why are serialization and deserialization useful?
to save data and use data
How do you deserialize a JSON string into a data structure using JavaScript?
by using the stringify method of the JSON object
How to you store data in localStorage?
by using the setItem method of the localStorage object
How to you retrieve data from localStorage?
by using the getItem method of the localStorage object
What data type can localStorage save in the browser?
JSON
When does the ‘beforeunload’ event fire on the window object?
Just before the page is unloaded/closed
What is a method?
A function that is part of an object
How can you tell the difference between a method definition and a method call?
A method definition starts with the function keyword
A method call starts with the object name and has an argument list in parenthesis
Describe method definition syntax (structure).
functionKey: function optionalName(optionalParameters) {code block}
Describe method call syntax (structure).
objectName.Function()
How is a method different from any other function?
It is apart of an object
What is the defining characteristic of Object-Oriented Programming?
Abstraction
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, inheritance, and polymorphism
What is “abstraction”?
simplifying complex ideas in to simple ones
What does API stand for?
Application programming interface
What is the purpose of an API?
To allow other people to interact with your program
What is this in JavaScript?
An implicit parameter a part of all functions
What does it mean to say that this is an “implicit parameter”?
It is included in all functions even if it was never declared
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?
the object literal, character
Given the above character object, what is the result of the following code snippet? Why?
‘it’s-a-me Mario!’ because this.firstname is referring to the object character whose firstName property is mario
Given the above character object, what is the result of the following code snippet? Why?
it’s-a-me, undefined! because you are just assigning the method definition to the var hello and not the object, so hello.firstName is undefined
How can you tell what the value of this will be for a particular function or method definition?
this will be undefined as it is not determined at definition
How can you tell what the value of this is for a particular function or method call?
by looking at what ever object’s property or method is when it is called
What kind of inheritance does the JavaScript programming language use?
prototypal
What is a prototype in JavaScript?
An object that delegates its methods and properties to 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?
Because the object’s prototype contains them
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
At it’s prototype
What does the new operator do?
Creates a blank object, adds __proto__ properties, binds the new instance to this, and returns this
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof operator do?
returns a boolean if the instance on the left contains the prototype on the right
What is a “callback” function?
A callback function is a function that is 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?
with the setTimeout function
How can you set up a function to be called repeatedly without using a loop?
with the setInterval function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0 (instant)
What do setTimeout() and setInterval() return?
a timeoutID
What is AJAX?
Ajax is a technique for loading data into part of a page
without having to refresh the entire page.
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 both inherit from the addeventlistener method
What is Array.prototype.filter useful for?
It creates a new array based on filters you put
What is Array.prototype.map useful for?
applying transforms to a whole array
What is Array.prototype.reduce useful for?
To reduce an array to a single value