Javascript Flashcards
What is the purpose of variables?
a way to store values
How do you declare a variable?
var and its name
How do you initialize (assign a value to) a variable?
with the assignment operator = then semicolon ;
What characters are allowed in variable names?
The period, the underscore, and the characters $, #, and @
What does it mean to say that variable names are “case sensitive”?
capitalizing letters change the name
What is the purpose of a string?
assign text to a value
What is the purpose of a number?
to assign a value a number to manipulate
What is the purpose of a boolean?
assign a value as either true or false
What does the = operator mean in JavaScript?
assignment
How do you update the value of a variable?
use the name with a new value assigned
What is the difference between null and undefined?
null means intentionally left blank and undefined means there is no value assigned
Why is it a good habit to include “labels” when you log values to the browser console?
so you know what the value your seeing represents
Give five examples of JavaScript primitives.
undefined , null , boolean , string and number
What data type is returned by an arithmetic operation?
integer value
What is string concatenation?
adding strings together to combine them
What purpose(s) does the + plus operator serve in JavaScript?
addition and concatenation
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
adds the value on the right to the variable on the left
What are objects used for?
containers for named values
What are object properties?
names that separate objects from others
Describe object literal notation.
an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last
How do you remove a property from an object?
use the delete operator before the object name
What are the two ways to get or update the value of a property?
bracket notation name[‘name2’], and dot notation name.name2
What are arrays used for?
when you want to store an ordered list of values
Describe array literal notation.
when you define an array using empty brackets
How are arrays different from “plain” objects?
they are ordered
What number represents the first index of an array?
0
What is the length property of an array?
how many entries there are in the array
How do you calculate the last index of an array?
you subtract 1 from the length since its index starts at 0
What is a function in JavaScript?
a block of code designed for a task that can be reused
Describe the parts of a function definition.
the arguments line and then the code block or body of the function
Describe the parts of a function call.
the name of the function and the arguments being passed
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition has the word function Infront of it as well as the body or declaration block of what the function is doing.
What is the difference between a parameter and an argument?
parameter if for the definition and argument is what is being sent to the function when it’s called
Why are function parameters useful?
they describe what the expected argument is
What two effects does a return statement have on the behavior of a function?
it ends the execution of the function and also returns controls what is given back to the calling function
Why do we log things to the console?
so developers can temporally see the output of something
What is a method?
actions that can be performed on objects
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?
pop() method
How do you round a number down to the nearest integer?
floor() method
How do you generate a random number?
Math.random() then multiply the result by array length or another value
How do you delete an element from an array?
splice()
How do you append an element to an array?
append()
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?
no, call the original string
Roughly how many string methods are there according to the MDN Web docs?
30-40
Is the return value of a function or method useful in every situation?
no, sometimes you just want the function to preform an action
Roughly how many array methods are there according to the MDN Web docs?
30-40
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?
to run code if a condition is met
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
the word if, the condition it parentheses, the code that will run if the condition is met
What are the three logical operators?
AND, OR, NOT
How do you compare two different expressions in the same condition?
logical operators ( &&, ||)
What is the purpose of a loop?
to repeat a process multiple times
What is the purpose of a condition expression in a loop?
to define how many iterations the statement will have
What does “iteration” mean in the context of loops?
times code is run
When does the condition expression of a while loop get evaluated?
after the initialization or after previous loop final expression
When does the initialization expression of a for loop get evaluated?
one time when the loop starts
When does the final expression of a for loop get evaluated?
after the code block
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 1 to the variable
How do you iterate through the keys of an object?
with a for in statement
Why do we log things to the console?
so we can check the value of an item in the document
What is a “model”?
a representation of a proposed structure
Which “document” is being referred to in the phrase Document Object Model?
HTML
What is the word “object” referring to in the phrase Document Object Model?
the elements and attributes and text
What is a DOM Tree?
the elements with the attributes and children branching off
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID and querySelector
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
so you don’t have to look for it again
What console method allows you to inspect the properties of a DOM element object?
.dir
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so it loads all other elements first before inspecting the document
What does document.querySelector() take as its argument and what does it return?
an element and returns the first one to match the name given
What does document.querySelectorAll() take as its argument and what does it return?
it returns all elements with the given name
Why do we log things to the console?
to check the value of a variable throughout the code
What is the purpose of events and event handling?
so that users can interact with a webpage making it less static
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?
a name that refers back to the definition instead of calling it immediately
What object is passed into an event listener callback when the event fires?
an event
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
a reference to an object. check mdn
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
ones a callback function that doesn’t run the code immediately while the other is calling it right away
What is the className property of element objects?
it sets the class attribute of the element given
How do you update the CSS class attribute of an element using JavaScript?
className
What is the textContent property of element objects?
how you get the value of the text of an element
How do you update the text within an element using JavaScript?
get the element and assign it with textConent property
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 because you’d have to go through html strings, since html doesn’t have values
Why is storing information about a program in variables better than only storing it in the DOM?
it make it much easier to make effects since all the information is already there
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?
prevents the event default behavior
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 property
What property of a form control object gets and sets its value?
value
What is one risk of writing a lot of code without checking to see if it works so far?
it can be hard to see where the code went wrong
What is an advantage of having your console open when writing a JavaScript program?
see if the code has any errors or check values
Does the document.createElement() method insert a new element into the page?
no it just creates it
How do you add an element as a child to another element?
append or appendChild
What do you pass as the arguments to the element.setAttribute() method?
the name of the attribute and the value you want it to be
What steps do you need to take in order to insert a new element into the page?
create the element then append it
What is the textContent property of an element object for?
to get or set the text for an element
Name two ways to set the class attribute of a DOM element.
element. className
element. setAttribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you can reuse them and test the easier
Give two examples of media features that you can query in an @media rule.
width/height, orientation
Which HTML meta tag is used in mobile-responsive web pages?
viewport meta tag
What is the event.target?
object the event is on
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 a css selector and returns the closest parent
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 it to the parent with and if statement to select correct elements
What is the event.target?
element where the event occurs
What is the affect of setting an element to display: none?
the document treats it like it doesn’t exist