JavaScript Flashcards
What is the purpose of variables?
to store values to be called upon later
how do you declare a variable
using a keyterm (var, let, const)
how do you initialize a variable?
using an equal sign
what characters are allowed in variable names?
$, _, letters (cap and no cap), numbers (cannot start with a number), non keyword (e.g. var), - , .
What does it mean by variables are case-sensitive?
var Cat and var cat are two separate variables
what is the purpose of a string?
to store letters/words/phrases values
what is the purpose of a number?
to store number values
what is the purpose of a boolean
to store true and false values (values that can only be 1 of the 2)
what does the = operator mean in JS?
a value is being assigned to something else
how do you update the value of a variable
assign it a new value
what is the difference between null and undefined?
null is user-defined emptiness, it is intentional and has to be assigned by the programmer (intentional emptiness)
undefined can be assigned by JS to create absence (usually not assigned by programmer)
Why is it a good habit to include labels when you log values to the browser console?
so you know which value you are logging and potentially where to find it
what are objects used for?
used to group similar properties together for ease of calling the values
what are object properties?
Object properties are variables within an object, usually are related or similar to the other properties within an object.
what are two ways to get or update the value of a property?
dot notation and bracket notation
object.property
object[‘property’]
what data type is returned from an arithmetic operation?
numbers
What is string concatenation?
addition for string values
what purpose does the + operator have in JS?
adds two values together
What data type is returned from using comparator operators? (greater than, less than, ===)
boolean (true/false)
what does the += operator do?
takes the existing value of a variable, adds another value to it, then assigns the new value back to it.
what are objects used for?
used for grouping up similar properties with values together for ease of access later
What are arrays used for ?
creating a numbered list of values, number of items within an array can be modified
describe array literal notation
[ value, value, value, value]
how are arrays different from “plain” objects?
arrays have a numbered index, order, and use [ ].
What number represents the first index of an array?
0
what is the length property of an array?
counts the number of indexes inside an array.
how do you calculate the last index of an array?
array[array.length - 1]
describe object literal notation
{ prop: value, prop: value, prop: value }
how do you remove a property from an object
using delete operator, followed by the object.property
delete object.property
what is a function?
reusable block of code that is able to receive different inputs and return different outputs based on those inputs
what is an expression?
a block of code or work that the computer needs to perform before it can assign it as a value.
what are the parts of a function definition?
keyword (function), optional function name, optional paramater list, { }, optional return
what are the parts of a function call
function name( ), argument function(argument)
What are the differences between a function call and function definition?
function call has argument instead of parameter, does not have the function keyword, no function code block.
What is the difference between a parameter and argument?
parameter is a placeholder for when defining a function, argument is a value that is actually passed through the function when calling the function.
Why are function parameters useful?
They are a placeholder for values to be passed through a function, let the user of the function know what arguments are allowed in the function
What two effects does return have on the function?
ends the function and pushes results of the work done inside the function outside so that it can be accessed
why do we log things in the console?
to check for errors in the code/ debugging
what is a method?
a method is a function that exists as a key in an object
How is a method different from any other function?
theyre effectively the same, a method just exists inside an object
how do you remove the last element of an array?
pop method of the array object
array.pop()
How do you round a number down to the nearest integer?
Math.floor() or Math.trunc()
How do you generate a random number?
Math.rand();
How do you delete an element from an array?
string.splice(index position to start, number of elements to remove, any elements to add starting from the splice start position)
How do you append an element to an array?
array.push(element)
how do you break a string up into an array
string. split(what is being split)
e. g. ‘ ‘ would split it at every space, ‘’ would split at every character
how do you capitalize all the letters in a string?
string.toUpperCase();
Do string methods change the original string? how would you check
no, console.log
how many string methods are there according to MDN web docs?
~30
Is the return value of a function or method useful in every situation?
some functions/methods do not need a return
how many array methods are there according to mdn docs?
~40
what three letter acronym should you always include in your google search about a JS method or CSS property?
mdn
6 examples of comparison operators
greater than, less than, greater than and equal to, less than and equal to, absolutely equal to, equal to (dont use), absolutely not equal to, equal to
what data type do comparison expressions evaluate to ?
boolean
what is the purpose of an if statement?
create conditionals
is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement
if keyword, followed by a condition, followed by the if-code block. condition is made up of two operands with an operator sandwiched between
What are the three logical operators?
and, or, not - &&, ||, !
how do you compare two different expressions in the same condition?
using $$ or || (and / or)
What is the purposed of a loop?
To run a designated block of code a number of times
what is the purpose of a condition expression in a loop?
It allows for the computer to know when to end the loop (once the condition fails)
What does iteration mean in the context of a loop?
each run through the loop is a single iteration
when does the condition of a while expression for a while loop get evaluated?
Before running the code inside the while loop code block.
When does the initialization expression of a for loop get evaluated?
when the loop is first read by the computer, it will initialize the initialization expression. Only done once, before the first condition expression
What is a falsey or truthy value?
values that JS coerces into a boolean value when placed in the context of a conditonal statement.
What are falsey values?
false, undefined, ‘empty string’, “empty string”, 0, -0, NaN, 0n, null, undefined
What are nodelists
an object created from using querySelectorAll, not a dom element, is a list of dom elements to prevent change to them. it is an array-like object.
what does the $ sign mean typically in dom?
stylistic choice; if used, all dom elements should be named using a $ in order to separate it from other variables. (should be used for the dom element rather than the content - only attached to values assigned to from query selector and queryselectorall)
what is event handling?
code that is executed to respond when an event has happened, to “handle” that event ( most events will be user controlled, but some can be controlled by time etc)
something to do in response to an event occurring
difference between event listener and handler?
event listeners designed to wait for an event to occur, and then invokes a list of functionalities that then occurs when the event occurs, event listeners call the event handler when the event occurs
when does the condition expression of a for loop get evaluated?
after the variable is declared, but before the code inside the for loop declaration block is run.
when does the final expression of a for loop get evaluated?
after the for loop code block gets executed, before the condition expression kicks in again
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 variable value by 1
how do you iterate through the keys of an object
for in loops
why do we log things to the console?
to make sure our expectations meet our results/ check for bugs in the code
What is a ‘model’
a representation of something, a smaller version that is equally if not as detailed as the original
which document is referred to in the phrase Document Object Model
html
What is the word object referring to in the phrase Document Object Model
the document object refers to the virtual object formed by js to interact with the html/css code
What is a DOM Tree
a model of dom in a tree structure, allows you to find the relationships between each node/element and therefore how to access each one
give two examples of document methods that retrieves a single element from the DOM
document.querySelector(), getElementByID()
document method that retrieves multiple elements form the DOM at once
document.querySelectorAll();
Why might you want to assign the return value of a DOM query to a variable?
to make it easier to access for later uses instead of having to recall it again everytime
What console method allows you to inspect the properties of a DOM element object?
console.dir()
why would a script tag need to be placed at the bottom of the HTML element instead of the top?
The DOM is created at the instance the script is run, if the script is before the HTML code, nothing will be read. The DOM is loaded in after all the css and html is loaded into the page
What does document.querySelector take as its argument and what does it return
it takes a css. selector and returns the first child element of that selector
What does document.querySelectorAll() take as its argument and what does it return?
it takes a css selector as its argument and returns all elements of that type as a nodelist
what is the purpose of events and event handling?
to allow for user interactivity with the webpage
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 functoin?
a function that is passed through another function as an argument, and then invoked inside the outer function to complete action or routine
what object is passed into an event listener callback when the event fires?
an event object
What is the event.target?
a reference to the object that was targeted when the event was dispatched
what is the difference between
element.addEventListener(‘click’, handleClick)
and
element.addEventListener(‘click’,handleClick())?
the second one calls the handleClick function, causing it to execute it immediately - second one waits until the ‘click’ event occurs before calling the function.