JavaScript Flashcards
What is the purpose of variables?
to store values/information
and we are able to refer/call this variable later!
i.e to store information for the future
How do you declare a variable?
use keyword (“var”, “let”, “const”)
followed with the name of variable and end with semicolon “;”
ex) var name;
How do you initialize (assign a value to) a variable?
single equal sign (assignment operator)
start with variable name following with an equal sign (=) and the value you want to assign to variable. end with semicolon
ex) Name = “Monique”;
- but usually will declare and assign variable in one step!!
ex) var Name = “Monique”;
What characters are allowed in variable names?
numbers, letters, dollar sign ($), underscore (_)
**but variables cannot start with numbers!!!
What does it mean to say that variable names are “case sensitive”?
lowercase and uppercase letters are different (even if its same letter)
(ex) Name, name –> are two different variable names
What is the purpose of a string?
to store letters and other characters
a sequence of a set of characters
js wont care about what is in strings
What is the purpose of a number?
to store numeric values
(that we usually do calculations with, mathematical operations)
financial information a good example to use numbers but not smt like a zipcode (b/c we arent usually doing mathematical operations with zipcode values)
What is the purpose of a boolean?
purpose is to make decisions
it is able to represent if something is or isn’t (binary)
What does the = operator mean in JavaScript?
assignment operator
used to assign a value to a variable
assign: to store information to a variable (name)
How do you update the value of a variable?
don’t use keyword!! (if variable already exists)
write variable name with equal sign (assignment operator) and new variable value
What is the difference between null and undefined?
both are value types
null: value that is assigned and purposely/intentionally left blank/empty
value that can only appear b/c DEVELOPER purposely assigned it to something
ex) optional inputs on a website from user (we still need to store data about user, so can use null in the optional inputs user did not fill out)
undefined: value that is assigned by BROWSER
value that can come from anywhere; if undefined pops up, need to investigate?
Why is it a good habit to include “labels” when you log values to the browser console?
to give clarity
to know if the correct value is being assigned to correct variables (debugging or double checking when you go back to code)
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 or more strings together to create one string
(addition but for strings)
*concatenation does not change the value of the individual strings”
What purpose(s) does the + plus operator serve in JavaScript?
mathematical operations (summing), string concatenation
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
shorthand operator
adds a value to an already existing variable and reassigns the resulting value to the same variable
ex) var name = “Monique”;
name += “ Chang”;
name variable's value would now be Monique Chang, and not Monique
What are objects used for?
to group together data, functions, etc that are related to each other
create multiple groups of organized data
(are able to use same property names for multiple objects)
What are object properties?
individually named data inside objects
(are variables essentially)
Describe object literal notation.
object name = { };
object name = {
property name: property value,
property name: property value,
property name: property value
};
*example of key/value pair
How do you remove a property from an object?
use delete.”object name”
delete operator
What are the two ways to get or update the value of a property?
use dot notation or bracket notation
objectname.objectpropertyname/key
objectname[“objectpropertyname.key”]
*bracket notation wont work for methods!
*bracket notation useful b/c in dot notation, js perceives anything after dot as a string
so variables, numbers etc cant work with dot notation.
. notation code reading
[ ] notation code reading
. = “of”
[ ] = “at”
What are arrays used for?
storing/grouping a set of related values
(helpful when you don’t know how many values there will be)
allows us to group together like data types and put into “lists”
so that they are organized and we can deal with them one at a time
Describe array literal notation.
array name = [ ];
array name = [
“value”,
“value”,
value
];
*value can be any data type!
How are arrays different from “plain” objects?
arrays have a order (index numbers; numerically indexed)
no property names (property names are basically the index numbers)
are able to know how many pieces of data is being stored (length property)
need to use method (push) to add data to array (not assign directly like objects)
What number represents the first index of an array?
zero
What is the length property of an array?
tells you the length of the array (how many values/items are in the array)
different from index numbers!!
arrayname.length
How do you calculate the last index of an array?
can use length property to find length of the array and subtract 1 to get last index num of the array
(b/c index numbers start at 0 so the last index number will be length of array minus one)
data models
objects in arrays
arrays in objects
organizing large chunks of data?
What is a function in JavaScript?
a block of code that contains set of instructions that gets executed when the function is called
take in values –> spit out values based on values provided
make change to something, somewhere
Describe the parts of a function definition.
function keyword, optional name, optional parameter(s), code block, optional return word (in code block)
Describe the parts of a function call.
function name with arguments(may or may not have) in parentheses
*if no arguments, still need ( )
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call: function name, arguments
function definition: function keyword, parameters, code block,
What is the difference between a parameter and an argument?
parameter: placeholder for arguments
argument: what you actually put into function to execute the code
Why are function parameters useful?
parameters give us the ability to have mutability based on data
don’t have to write multiple functions
gives us functionality
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a value we can use
Prevents any more code in the function’s code block from being run entirely.
(even if theres more code after return statement in the code block)
an expression in JS
needs to be evaluated to reach a value
Why do we log things to the console?
debugging tool
double check code, but soley to check
What is a method?
a type of function
How is a method different from any other function?
methods are attached to objects
How do you remove the last element from an array?
pop( ) method
*will return that element you removed
How do you round a number down to the nearest integer?
Math.floor ( )
How do you generate a random number?
Math.random( );
0 to 1 (but 1 isn’t included)
How do you delete an element from an array?
splice method
How do you append an element to an array?
push method
*returns length of array
*append means to add at the end
How do you break a string up into an array?
split method
*returns an array
Do string methods change the original string? How would you check if you weren’t sure?
no they do not
can use console.log(“the string”) to check
Roughly how many string methods are there according to the MDN Web docs?
a lot!
Is the return value of a function or method useful in every situation?
not always
Roughly how many array methods are there according to the MDN Web docs?
a lot!
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?
booleans
What is the purpose of an if statement?
to check condition to decide whether to run a certain code block or not
Is else required in order to use an if statement?
no
*if else is included means something will always happen
we dont necessarily want something to occur always
Describe the syntax (structure) of an if statement.
if keyword, condition, code block
What are the three logical operators?
and(&&), or ( | | ), not ( ! )
How do you compare two different expressions in the same condition?
use “and” or “or”
truthy values and falsey values (for conditionals)
truthy: assumed to have values
*infinte amount of truthy values (everything that is not truthy)
falsey: emptiness
*(not that many)
false, 0, empty strings, null, undefined, NaN
What is the purpose of a loop?
a set of code can be run over and over again
gives us ability to repeat functionality
What is the purpose of a condition expression in a loop?
if condition is met: loop continues
if condition is not met: loop stops
what decides when to stop a loop
What does “iteration” mean in the context of loops?
one time a loop code block is run
When does the condition expression of a while loop get evaluated?
before code block is executed (before each itieration)
When does the initialization expression of a for loop get evaluated?
one time before everything
When does the condition expression of a for loop get evaluated?
during first iteration: after initialization
during every following iteration: right at start of loop (before code block runs) / after final expression
When does the final expression of a for loop get evaluated?
before the condition runs again
after the code block runs
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 one to the variable it is applied to (and saves that value in that variable)
*count++ : using count and then incrementing
*++count : incrementing count value before using
How do you iterate through the keys of an object?
use for in loop
Why do we log things to the console?
debugging, checking if certain code works properly. etc.
What is a “model”?
representation of something
Which “document” is being referred to in the phrase Document Object Model?
the html document (document that represents the entire page)
What is the word “object” referring to in the phrase Document Object Model?
JS objects (b/c DOM tree is made of objects)
Document Object Model: is a JavaScript object modeling the HTML document’s content
What is a DOM Tree?
form of organization of data
all data necessary to make a single element and all of it’s content (child elements, attributes, etc)
Give two examples of document methods that retrieve a single element from the DOM.
getElementById( ), querySelector( )
*querySelector( ) use this!!!!!!
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementsByClassName( ), getElementsByTagName( ), querySelectorAll( )
Why might you want to assign the return value of a DOM query to a variable?
if you are going to use the return value of the DOM query again (multiple times)
don’t have to keep searching for it each time u use it
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
dir = directory
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
HTML document loads code from top to bottom, so need to put at bottom so body element needs to load first
the browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
argument: css selector
returns: first element with the argument css selector
What does document.querySelectorAll() take as its argument and what does it return?
argument: css selector
returns: all elements with the argument css selector (in a NodeList)
NodeList
looks like an array but is not the same
its an array-like object (but cant adjust like an array)
What is the purpose of events and event handling?
events: to be aware when certain action happens
event handling: to allow us to do smt in response to event (interactiveness)
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 function passed as an argument in another function
What object is passed into an event listener callback when the event fires?
event object
a object js makes that holds all relevant info about that certain event (when it is fired)
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
the place event occurred from
use inspect or mdn
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
handleClick executes when event happens
handleClick( ) is being called right when page loads
code in function code block isn’t correct
and when you click button, returns undefined (so nothing happens)
What is the className property of element objects?
property that contains value of class attribute of that element
How do you update the CSS class attribute of an element using JavaScript?
use className property to change value of the class attribute of that element
get element -> assign new value (using assignment operator)
What is the textContent property of element objects?
property that contains text content of certain element
How do you update the text within an element using JavaScript?
use textContent property to change text content (using assignment operator)
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?
easy access to elements w/o having to query/search for it everytime
data is readily accessible in the language we are writing
better to use data w/in language you are working 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 form element?
submit
submit event is an event on FORM element, NOT submit button(submit button triggers event)
What does the event.preventDefault() method do?
prevents default action for an event from being taken as it normally would be.
What does submitting a form without event.preventDefault() do?
automatically reloads page
preventDefault will always be present when using submit events
What property of a form element object contains all of the form’s controls.
elements
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?
missing errors and having to now go through a lot of code to fix errors
not catching errors early and not being able to see clearly where error is from
What is an advantage of having your console open when writing a JavaScript program?
can see how certain code affects webpage/browser in live time
can check code right away