js Flashcards
What is the purpose of variables?
store data; store value in variables that can be accessed; data that persists (accessible in the future; “data permanence”; saved for later)
How do you declare a variable?
using the keyword var
How do you initialize (assign a value to) a variable?
var keyword, name of the variable, assignment operator (equal sign), value of the variable
What characters are allowed in variable names?
letters, dollars, numbers (can’t start with a number), underscore
What does it mean to say that variable names are “case sensitive”?
required to match to function/work
What is the purpose of a string?
store data with text/series of characters; sequence of characters used to represent text
What is the purpose of a number?
numeric data type; 64 bits of memory
What is the purpose of a boolean?
logical data type that can have only the values true or false, i.e., boolean conditionals are often used to decide which sections of code to execute (such as in if statements) or repeat (such as in for loops); make decisions (do or don’t; yes or no)
What does the = equal sign operator mean in JavaScript?
assigning a value or something
How do you update the value of a variable?
reassign with new value
Why is it a good habit to include “labels” when you log values to the browser console?
debugging purposes; gives context to what data types you are working with
Give five examples of JavaScript primitives.
string, number, boolean, undefined, null, symbol (produces an anonymous, unique value), bigint (aka long arithmetic, numeric data type; allows to process much greater numbers than can be fit in standard data types); all primitives are immutable (can’t be altered), that is not an object or method
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combination of two or more string values; can include concatenating to a number
What purpose(s) does the plus operator serve in JavaScript?
math or concatenation
What data type is returned by comparing two values (< less than, > greater than, === strictly equal, etc)?
boolean
What does the += “plus-equals” operator do?
adds to the value to the right to the value to the left and the result is the new value
What are objects used for?
data type; store various keyed collections and more complex entities
What are object properties?
association between name and value
Describe object literal notation.
a variable assigned to an object illustrated by opening and closing braces with properties and their respective values within; an array of key:value pairs
How do you remove a property from an object?
delete operator using the keyword delete and the object with it’s respective property value that you want to delete
What are the two ways to get or update the value of a property?
dot notation and bracket notation
What are arrays used for?
store data with a specific index; ordered
Describe array literal notation.
square brackets
How are arrays different from “plain” objects?
arrays are objects but objects aren’t arrays
What number represents the first index of an array?
0
What is the length property of an array?
array.length; number of entries in the array
How do you calculate the last index of an array?
array[array.length - 1]
What is a function in JavaScript?
type of object that can be called; set of reusable code
Describe the parts of a function definition.
function keyword, optional name for that function, parentheses around parameters (zero to infinity), curly braces for the function body, function body
Describe the parts of a function call.
function name followed by parentheses; no arguments if not necessary
When comparing them side-by-side, what are the differences between a function call and a function definition?
arguments being passed through, function def has a function body
What is the difference between a parameter and an argument?
parameter is a variable definition; argument is set data to be passed to the respective parameters
Why are function parameters useful?
pass info to function
What two effects does a return statement have on the behavior of a function?
produces a value in the function body to be sent outside the function for the function to be called when necessary
Why do we log things to the console?
.
What is a method?
a function stored within a property of an object;
How is a method different from any other function?
.
How do you remove the last element from an array?
array[array.length - 1]
How do you round a number down to the nearest integer?
by calling the floor method of the math object function
How do you generate a random number?
by calling the random method of the math object function
How do you delete an element from an array?
using the pop method of the array object
How do you append an element to an array?
using the push (append), using the (prepend)
How do you break a string up into an array?
using the splice method
Do string methods change the original string? How would you check if you weren’t sure?
they do not; console log output; strings are immutable (can’t be altered)
Roughly how many string methods are there according to the MDN Web docs?
a lot (around 30)
Is the return value of a function or method useful in every situation?
no, i.e., splice method (we care about the work, not the return)
Roughly how many array methods are there according to the MDN Web docs?
a lot (~36)
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.
equal to, strictly equal to, less than, greater than, not equal to, greater than or equal to
What data type do comparison expressions evaluate to?
strings, numbers, objects
What is the purpose of an if statement?
compare
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, comparison statement, body
What are the three logical operators?
or, and, nullish
How do you compare two different expressions in the same condition?
use a logical operator(?)
What is the purpose of a loop?
quick and easy way to do something repeatedly
What is the purpose of a condition expression in a loop?
keeps the loop running or stops the loop
What does “iteration” mean in the context of loops?
when the same procedure is repeated multiple times
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 at the beginning of the loop (before anything)
When does the condition expression of a for loop get evaluated?
once before every iteration (before each)
When does the final expression of a for loop get evaluated?
after each iteration; condition expression becomes false
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
the break keyword? which doesn’t work in a for…in loop
What does the ++ increment operator do?
adds one to the operand and returns it
How do you iterate through the keys of an object?
for… in loop, i.e., for keys in object, or using the keys method of object Object
what are the parts of a for loop
[initialization] - this expression is where you initialize a variable that will be used as the counter; this counter will be updated in the incrementer and checked in the condition
[condition] - if this expression evaluates to true, we run another “cycle” of the loop, if false, we stop and exit the loop; we call each “cycle” in a loop an iteration.
[incrementer] - this expression is evaluated after every iteration of the loop; this step should bring the counter that was created in the [initialization] closer to a [conditional] that evaluates to false
what are the steps of a for loop
step 1: initialize the counter
step 2: evaluate the condition - if it evaluates to false, exit the loop / if it evaluates to true, move forward to step 3
step 3: run all the code inside the body of the loop
step 4: increment the counter and go back to step 2
what are the while loop parts
[initialization]
while ([conditional]) // do something...
[incrementer]
looping keywords
break (terminates the loop) and continue (skips the current loop iteration)
what are some useful methods
Array.prototype.length Array.prototype.indexOf(element) Array.prototype.unshift(element) Array.prototype.shift() Array.prototype.push(element) Array.prototype.pop() Array.prototype.concat(other_array) Array.prototype.slice(startIndex, endIndex) Array.prototype.length (property) Array.isArray(obj)
useful string methods
string.split(separator) is a very helpful string method that splits a string into an array of substrings on the separator and returns the new array; does not change the original string
What is fetch() and what does it return?
- a promise that resolves to a response object
- a promise which is fulfilled once the response is available
- promise does not reject on HTTP errors
- only rejects on network errors
- use then handlers to check for HTTP errors
What is the default request method used by fetch()?
get