JavaScript Flashcards
What is the purpose of variables?
to store information
How do you declare a variable?
var name
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
letters number _ $
cannot start with a number
What does it mean to say that variable names are “case sensitive”?
letters have to match so the comp understands
What is the purpose of a string?
to store word or sentences
What is the purpose of a number?
to store numbers
What is the purpose of a boolean?
true or false
What does the = operator mean in JavaScript?
assigned
How do you update the value of a variable?
the var name and assignment op then the new value
What is the difference between null and undefined?
null means nothing. undefined means a variable has been declared but not defined yet.
Why is it a good habit to include “labels” when you log values to the browser console?
to help with debugging and to see what your console is logging
Give five examples of JavaScript primitives.
undefined , null , boolean , string and number
What data type is returned by an arithmetic operation?
a number
What is string concatenation?
to combine strings together
What purpose(s) does the + plus operator serve in JavaScript?
adds values together
What data type is returned by comparing two values (, ===, etc)?
a Boolean
What does the += “plus-equals” operator do?
adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left.
What are objects used for?
to holds multiple values..
they are used to model real world objects
What are object properties?
a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method
Describe object literal notation.
var example = { }
How do you remove a property from an object?
delete keyword
What are the two ways to get or update the value of a property?
bracket or dot notation
What are arrays used for?
lets you store multiple values in a single variable.
Describe array literal notation.
var example = []
How are arrays different from “plain” objects?
arrays stores list
What number represents the first index of an array?
0
What is the length property of an array?
to see 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 or calculates a value
Describe the parts of a function definition
function functionName(parameter) { }
Describe the parts of a function call.
function name(argument)
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition has to have the keyword and { }
call is just name and ()
Why are function parameters useful?
allows us to store data in it
What two effects does a return statement have on the behavior of a function?
ends function execution and specifies a value to be returned
Why do we log things to the console?
to see if its working proper, debugging
What is a method?
methods are actions that can be performed on objects.
a function on a obj
How is a method different from any other function?
a method is associated with an object
How do you remove the last element from an array?
array.pop()
How do you round a number down to the nearest integer?
floor() Method of math obj
How do you generate a random number?
random() of math obj
How do you delete an element from an array?
array.splice()
How do you append an element to an array?
push() and unshift() 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 return a new string. to check console.log()
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
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.
=== , !==, > , < , <= , >=
Strictly equal, Strictly unequal, less than, greater than, less than or equal to , greater than or equal to
What data type do comparison expressions evaluate to?
booleans
What is the purpose of an if statement?
to evaluate or check a condition, to make a decision
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if () {
}
What are the three logical operators?
&& ,||, !
and , or , NOT
How do you compare two different expressions in the same condition?
with a logical operator
What is the purpose of a loop?
to do something repeatedly and as quickly as possible
What is the purpose of a condition expression in a loop?
to see if the code should run again
What does “iteration” mean in the context of loops?
the number of times it iterates
When does the initialization expression of a for loop get evaluated?
Runs before the first execution on the loop
When does the initialization expression of a for loop get evaluated?
it is the first thing ran
When does the final expression of a for loop get evaluated?
Expression that is run after every iteration
What does the ++ increment operator do?
adds 1
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break keyword
What does the ++ increment operator do?
adds 1, assigns the new value to the original
How do you iterate through the keys of an object?
for in loop
Why do we log things to the console?
seeing the data and for debugging
What is a “model”?
a representation of something
Which “document” is being referred to in the phrase Document Object Model?
document node
What is the word “object” referring to in the phrase Document Object Model?
different parts of the page loaded in the browser
What is a DOM Tree?
all the pieces of the dom
Give two examples of document methods that retrieve a single element from the DOM.
getElementById , querySelector
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName
Why might you want to assign the return value of a DOM query to a variable?
reduces the amount of times to query the dom
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
because the broswer needs to analyze all the html before the js
What does document.querySelector() take as its argument and what does it return?
takes a vaild css selector
its returns An HTML Element object representing the first element in the document that matches the specified set of CSS selectors,
What does document.querySelectorAll() take as its argument and what does it return?
takes a vaild css selector
its returns a static (not live) NodeList representing a list of the document’s elements that match the specified group of selectors.
Why do we log things to the console?
seeing the data and for debugging
What is the purpose of events and event handling?
to have different interactions
What do [] square brackets mean in function and method syntax documentation?
that its optional
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function?
a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
the event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
event.target tells where the event started. check in the console.log, more info in mdn web docs
What is the difference between these two snippets of code?
with the parentheses the function would run as the
page loads, instead of when the event fires
What is the className property of element objects?
sets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
example.className =
What is the textContent property of element objects?
allows us to get or set the text content
How do you update the text within an element using JavaScript?
example.textContent =
Is the event parameter of an event listener callback always useful?
no
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
more complicated!
Why is storing information about a program in variables better than only storing it in the DOM?
alot more control in variables
What is the event.target?
to target of the element that you interacted with
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What DOM element property tells you what type of element it is?
tagName
What does the element.closest() method take as its argument and what does it return?
takes selectors as the argument and returns the closest ancestor of the selected element
How can you remove an element from the DOM?
node.remove();
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
put the listener on the parent
What is the event.target?
to target of the element that you interacted with
What is the affect of setting an element to display: none?
its hides that element
What does the element.matches() method take as an argument and what does it return?
takes a selector and 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?
all the time
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?
event listeners for each tab and callback functions for those
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?
would have to manually do the checks
What is JSON?
json is a text-based data format following JavaScript object syntax. it allows different systems to exchange data
What are serialization and deserialization?
Serialization is turning an object into a stream of bytes so you can send it over the network.
Deserialization getting a stream of bytes and turning it into a obj
Why are serialization and deserialization useful?
allows us to commutate from different clients
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()