JAVASCRIPT Flashcards
What is the purpose of variables?
to store data (short-term memory)
it can change or vary each time a script runs
How do you declare a variable?
var keyword var name;
ex: var quantity;
Var (old school), const, and let (if it’ll change)
How do you initialize (assign a value to) a variable?
quantity = 3;
What characters are allowed in variable names?
Upper or lowercase letter, dollar sign, or underscore
you can use numbers, but not as the first character
What does it mean to say that variable names are “case sensitive”?
if it is capitalized, you need to capitalize it always in order for it to be recognized as the same variable
What is the purpose of a string?
to store text (always within quotation marks) – letters and other characters
What is the purpose of a number?
counting, math, representing quantities, etc
- determining size of screen
- moving position of element on a page
- setting amount of time an element should take to fade in
What is the purpose of a boolean?
true/false values
What does the = operator mean in JavaScript?
It’s an assignment operator, not an ‘equal to’
How do you update the value of a variable?
use the equal sign. make sure it comes after the initial declaration
Why is it a good habit to include “labels” when you log values to the browser console?
it makes it clearer what variable is being logged and in what order
Give five examples of JavaScript primitives.
string number boolean null undefined symbol
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combines separate strings together to form one string
What purpose(s) does the + plus operator serve in JavaScript?
to add one value to another
concatenates
What data type is returned by comparing two values (, ===, etc)?
boolean (true or false)
What does the += “plus-equals” operator do?
it’s a shorthand for the addition assignment
if you know you are going to take the sum and add to it, you can use this
instead of x = x + y, it becomes x += y
What are objects used for?
group together a set of variables and functions to create a model of something
ex: object is hotel
What are object properties?
variables
ex:
- name:
- rooms:
- booked:
Describe object literal notation.
var object = { key: value, key: value, method; }
each is separated with a comma, except for the last one
How do you remove a property from an object?
delete object.property;
to clear the value:
object.property = ‘’;
What are the two ways to get or update the value of a property?
dot notation
object.property = new value;
brackets notation
object[‘property’].= new value
What are arrays used for?
working with a list or set of values that are related to each other. especially when you don’t know how many items/values there will be
Describe array literal notation.
var arrayName;
arrayName = [‘value’, ‘value];
var color = arrayName[0]
How are arrays different from “plain” objects?
arrays are numerically indexed, whereas object properties are alphanumerically indexed
What is the length property of an array?
checks how many items are in an array
var numColors = array.length
What is the length property of an array?
checks how many items are in an array, numerically
var numColors = array.length
What is a function in JS?
a series of statements we can call to perform a task
Why do we log things to the console?
- to help debug
- visualize the data
What is a method?
a function, which is a property of an object
How is a method different from any other function?
object that already contains a function definition
What is the difference between a parameter and an argument?
A parameter is the variable local to the function
The argument is the actual value itself that you are inputting into the function call
Why are function parameters useful?
parameters are placeholders and already hold the value of the argument
What two effects does a return statement have on the behavior of a function?
- Causes the function to produce a value we can use in our program.
- Prevents any more code in the function’s code block from being run.
How do you calculate the last index of an array?
last index = array.length - 1
What is a method?
a function, which is a property of an object
an object reference to a function?
How do you break a string up into an array?
string.split(separator)
ex: string.split(‘ ‘)
if you
How do you remove the last element from an array?
array.pop();
How do you round a number down to the nearest integer?
Math.floor();
Is the return value of a function or method useful in every situation?
yes but no
for example when you are using methods to change an array, you may want to double check what element you are deleting, but not necessarily
Roughly how many array methods are there according to the MDN Web docs?
40-ish
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
How do you break a string up into an array?
string. split(separator)
ex: string.split(‘ ‘)
Do string methods change the original string? How would you check if you weren’t sure?
no because the value is assigned to a different variable
you can console log the original string to make sure
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.
> < >= <= != is not equal to == (preferable to strict method)
What data type do comparison expressions evaluate to?
booleans or true or false
What is the purpose of a condition expression in a loop?
to let the loop know when to stop running
Is else required in order to use an if statement?
no. nothing will run
Describe the syntax (structure) of an if statement.
keyword if (condition - operand, logical expression) { 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?
else if
or logical comparison operators
What is the purpose of a loop?
to run a code block various times, so long as the conditions are true
useful for iterating through an array
What is the purpose of a condition expression in a loop?
it instructs the code to run a specified number of times
What does “iteration” mean in the context of loops?
how many time the loop has run
When does the condition expression of a while loop get evaluated?
before each pass through the loop
When does the initialization expression of a for loop get evaluated?
once before the loop begins
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 each loop iteration
occurs before the next evaluation of condition
generally used to update or increment counter
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?
increases the counter by 1
How do you iterate through the keys of an object?
using a for… in loop
for (let key in object) {
}
Why do we log things to the console?
to make sure our code is working properly
to inspect and debug
What is a “model”?
a representation or replica of something
a data structure
Which “document” is being referred to in the phrase Document Object Model?
the web page being loaded
What is the word “object” referring to in the phrase Document Object Model?
different part of the page loaded in the browser window
What is a DOM Tree?
the model of the web page being loaded
made of objects
Give two examples of document methods that retrieve a single element from the DOM.
getElementById() - uses the value of an element’s id attribute (should be unique)
querySelector() - uses CSS selector and returns the first matching element
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName() - specific to class attribute - returns in live node list
querySelectorAll() - uses CSS selector to select all matching elements
Why might you want to assign the return value of a DOM query to a variable?
easier for when you are working with an element more than once
saves the browser from looking through the DOM tree to find the same elements again (caching the selection)
What console method allows you to inspect the properties of a DOM element object?
console.dir()
displays interactive list of all properties of specified JS object
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so that the browser loads all html data first
page loads to render all dom elements to work with
What does document.querySelector() take as its argument and what does it return?
argument - css selector
ex: querySelectorAll(‘li.hot’)
returns - first element that matches this
What does document.querySelectorAll() take as its argument and what does it return?
argument - css selector
ex: querySelectorAll(‘li.hot’)
returns - all elements that match the css selector syntax
node list, which returns one object for each element
instead of null, it will also return an empty node list
static node list
What is the purpose of events and event handling?
events = user interaction with web pages and sites
event handling allows us to design sites that are responsive, interactive, and give data about a user’s need
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?
element.addEventListener()
What is a callback function?
a function passed into another function as an argument
What is the event.target?
If you weren’t sure, how would you check?
Where could you get more information about it?
it tells you what the target of the event is
if you weren’t sure, you could open the source –> console –> event dropdown –> read the target attribute
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The second will not work because although it is the function name, there should not be parenthesis there (that will call the function)
What is the className property of element objects?
accesses the name of the class of the element
How do you update the CSS class attribute of an element using JavaScript?
$element.className = ‘hot-button cool’
What is the textContent property of element objects?
accesses the text content of the element
How do you update the text within an element using JavaScript?
document.querySelector(‘.click-count’).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?
it’s easier for programmers to access and work with
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?
cancels the event if it’s cancelable / the default action will not occur
ex: clicking on submit will not submit the form
What does submitting a form without event.preventDefault() do?
allows default behavior to occur
example: form values will be added to the end of the url
- it will refresh the page and you lose the data
- you will also be showing passwords in the URL, which is not secure
What property of a form element object contains all of the form’s controls.
HTMLFormElement.elements
What property of a form control object gets and sets its value?
$form.elements.name.value
What is one risk of writing a lot of code without checking to see if it works so far?
you have more to backtrack, console log, and look through
What is an advantage of having your console open when writing a JavaScript program?
you can test your code as you are writing it
What is the event.target?
returns the element/object that triggered the event
remember DOM – all elements are objects
What does the element.matches() method take as an argument and what does it return?
takes - a string containing CSS selectors
returns - true or false
How can you retrieve the value of an element’s attribute?
getAttribute() method of the element
$element.getAttribute(attributeName)
returns: a string containing the value of the attribute name
At what steps of the solution would it be helpful to log things to the console?
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?
add event listeners to each node, instead of just the one on the parent node – which bubbles events to child elements
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?
create event listeners for each tab and change class names
display: none
it’ll hide the element without deleting it
removes it from flow of the page
What is JSON?
JS Object Notation
text-based format for representing data
What are serialization and deserialization?
serialization - converting object into a string
deserialization - converting back into its native object
Why are serialization and deserialization useful?
- so it can be transmitted across the network easily
- stored as its own txt file
- sending data from server to client
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify() - converts JS object into a JSON string
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse() - converts JSON string back into a JS object
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?
json strings
When does the ‘beforeunload’ event fire on the window object?
when the window, the document and its resources are about to be unloaded (like refreshing or leaving the page)