JAVASCRIPT Flashcards
- How do youdeclarea variable?
var nameOfVariable
- How do you initialize (assign a value to) a variable?
nameOfVariable = sting, number, or boolean
- What characters are allowed in variable names?
letter, underscore, or dollar sign
- What does it mean to say that variable names are “case sensitive”?
superMan will be different than superman
- What is the purpose of a string?
for letters and other characters not considered code
- What is the purpose of a number?
to display numbers
- What is the purpose of a boolean?
to display whether something is true or false - works as switch
- What does the=operator mean in JavaScript?
does not mean equal, means assign
- How do you update the value of a variable?
variableName then assign new value
- What is the difference betweennullandundefined?
null is predictable, set by developer… undefined has no value
- Why is it a good habit to include “labels” when you log values to the browser console?
to identify what our log is
- 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?
adding two strings together
- What purpose(s) does the+plus operator serve in JavaScript?
to add two values, string and string, number and number, or mix
- What data type is returned by comparing two values (,===, etc)?
boolean
- What does the+=”plus-equals” operator do?
shorthand operator that adds right operand to left operand and assigns back to left operand
- What are objects used for?
to store information about something
- What are object properties?
variable in the confines of an object, place to store data that can be accessed
later
- Describe object literal notation.
{ property: value };
- How do you remove a property from an object?
delete operator, dot or bracket
- What are the two ways to get or update the value of a property??
dot notation or bracket notation
- What are arrays used for?
to store lists of similar data
- Describe array literal notation.
declare variable/array then assignment, then opening bracket for array, list properties, closing bracket of array
- What is thelengthproperty of an array?
property that tells how many pieces of data are inside array
- How do you calculate the last index of an array?
array.length - 1
- What is a function in JavaScript?
set of steps, instructions, code, that does a specific job
- Describe the parts of a functiondefinition.
function keyword, optional name, optional parameter, (), {, code to run, return}
- Describe the parts of a functioncall.
function name (arguments)
- When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
one has arguments one has parameters, function definition has code block
- Why are functionparametersuseful?
it allows a function to pass information
- What two effects does areturnstatement have on the behavior of a function?
returns data/information and replaces the function call - stops function
- What is a method?
methods are functions, property of an object
- How is a method different from any other function?
syntax of method and function differs also a method calls on 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
- How do you generate a random number?
Math.random() method
- How do you delete an element from an array?
splice() method
- 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?
they don’t, if you want to check, console log it
- Is the return value of a function or method useful in every situation?
No
- Give 6 examples of comparison operators.
, >=, <=, !, &&, ==
- What are the three logical operators?
&& ! ||
- What is the purpose of a loop?
to repeat a block of code as necessary
- What is the purpose of aconditionexpression in a loop?
sets the condition as for when the loop should stop
- What does “iteration” mean in the context of loops?
each time the code block runs
- Whendoes theconditionexpression of awhileloop get evaluated?
before each iteration
- Whendoes theinitializationexpression of aforloop get evaluated?
before the loop begins just once
- Whendoes theconditionexpression of aforloop get evaluated?
before each iteration takes place
- Whendoes thefinalexpression of aforloop get evaluated?
after each iteration takes place
- Besides areturnstatement, which exits its entire function block, which keyword exits a loop before itsconditionexpression evaluates tofalse?
break
- What does the++increment operator do?
increments value of variable by 1
- How do you iterate through the keys of an object?
for in loop
- Give two examples ofdocumentmethods that retrieve a single element from the DOM.
queryselector and getelementbyid
- Give one example of adocumentmethod that retrieves multiple elements from the DOM at once.
queryselectorall and getelementsbytagname and getelementsbyclassname
- Whatconsolemethod allows you to inspect the properties of a DOM element object?
console.dir();
- Why would atag need to be placed at the bottom of the HTML content instead of at the top?
because html docs load top to bottom, so javascript must be at bottom to retrieve html elements
- What doesdocument.querySelector()take as its argument and what does it return?
takes string with css selector and return element node
- What doesdocument.querySelectorAll()take as its argument and what does it return
takes string with css selector and returns a novelist
- Why do we log things to the console?
for debugging, to log values and variables, and for verification — but for Dom events to check if action happens
- Are all possible parameters required to use a JavaScript method or function?
no
- 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?
function passed as an argument for another function
- What object is passed into an event listener callback when the event fires?
the event object
- What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
the top one waits for the browser to call the function where as the bottom one runs calls the function as soon a page loads
- What is theclassNameproperty of element objects?
allows us to get and set class attribute
- How do you update the CSS class attribute of an element using JavaScript?
elementselected.className = ‘class name’
- What is thetextContentproperty of element objects?
get or sets text content of element object
- How do you update the text within an element using JavaScript?
element.textContent = ‘string’
- Is theeventparameter of an event listener callback always useful?
no
- Why is storing information about a program in variables better than only storing it in the DOM?
variables are easier to work with in javascript - condenses your dom work
- 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 when a user clicks the”submit”button within a?
submit
- What does theevent.preventDefault()method do?
prevents default behavior of event
- What does submitting a form withoutevent.preventDefault()do?
refreshes page
- What property of a form element object contains all of the form’s controls.
elements property
- What property of a form control object gets and sets its value?
value property
Does the document.createElement() method insert a new element into the page?
It does not, just creates it. Still needs to be assigned or inserted to page.
How do you add an element as a child to another element?
parent.appendChild(element)
What do you pass as the arguments to the element.setAttribute() method?
setAttrribute(nameofattribute, value)
What steps do you need to take in order to insert a new element into the page?
Query the parent element, create a new element, append new element to parent element
What is the textContent property of an element object for?
used to set text content for the HTML element or get the text content written inside that element
Name two ways to set the class attribute of a DOM element.
setAttribute(name,value) or document.className() method
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
reusable
What is the event.target?
element where the event originated from
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?
NEED
How can you remove an element from the DOM?
element.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?
add event listener to parent
What is the event.target?
element that triggered event, where event originated from
What is the affect of setting an element to display: none?
element removed from document flow, hides element - no longer visible
What does the element.matches() method take as an argument and what does it return?
Takes a css selector thats a string, returns boolean if it matches dom element
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?
At 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?
you would have to add event listeners to every new element
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’d have to write a condition for each tab
What are serialization and deserialization?
serialization: chunk of data that is sequenced
deserialization: sequenced row of data that is made easy to use
Why are serialization and deserialization useful?
serialization helps make complex data easily transmissible, deserialized data allows us to work with the data more efficiently
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(values), values ie object or array
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(string) method to return object or array
How do you store data in localStorage?
localStorage.setItem();
How do you retrieve data from localStorage?
localStorage.getItem();
What data type can localStorage save in the browser?
strings only
When does the ‘beforeunload’ event fire on the window object?
before the content of the page is loaded
What is amethod?
a function that’s a property of an object
How can you tell the difference between a methoddefinitionand a methodcall?
definition has function with code block where as call does not
Describe methoddefinitionsyntax (structure).
method name, colon, function keyword followed by parameter(s) and code block
Describe methodcallsyntax (structure).
object or element period method name parenthesis argument(s) being passed in
How is a method different from any other function?
method has a receiver whereas functions don’t
What is thedefining characteristicof Object-Oriented Programming?
OOP can contain both data and methods as properties
What are the four “principles” of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
the process of making something complex simple for our use
What does API stand for?
application programming interface
What is the purpose of an API?
holds contracts and comm with other applications or computer systems, defines functions and who does what