Javascript Flashcards
What is the purpose of variables?
to store information to be referenced/accessed later
How do you declare a variable?
use the keyword “var”
- var newVariable
- (also use const/let)
How do you initialize (assign a value to) a variable?
put an equals sign after the variable you want to initialize, and then put the value after the equals sign
e.g. newVariable = ‘new value’
What characters are allowed in variable names?
- variable must begin with: letter, $, or _. after that, numbers can be used as well
- cannot use keywords
What does it mean to say that variable names are “case sensitive”?
capital and lower casing matter
What is the purpose of a string?
stores a sequence of characters (text)
What is the purpose of a number?
gives us a numeric value as a constant to work with
What is the purpose of a boolean?
indicates two possible values (true/false)
What does the = operator mean in JavaScript?
assigns a value to the variable that’s on the left of the operator
How do you update the value of a variable?
varName = ‘new updated value’
What is the difference between null and undefined?
- null is an object with the assigned value of “no value”
- undefined is a type, where the variable is undeclared or undefined (not been given a value)
- in your code, you should not use “undefined”. undefined is generally used for debugging
Why is it a good habit to include “labels” when you log values to the browser console?
so that you know the context for the printed value
Give five examples of JavaScript primitives.
string, number, boolean, undefined, null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combination of 2+ values
What purpose(s) does the + plus operator serve in JavaScript?
adding number values or concatenation
What does the += “plus-equals” operator do?
adds the right-hand value onto the left-hand value and assigns it to the left-hand value
—> (x += 5) is the same as (x = x+5)
What are objects used for?
used to hold a collection of properties and methods
What are object properties?
a name-value pair that tells us more about the object
Describe object literal notation
objectName = { name: value, name2: value2, name3: value3 }
How do you remove a property from an object?
delete objectName.propertyName
What are the two ways to get or update the value of a property?
dot notation, bracket notation
- dot notation is very literal. property identifiers cannot be a variable, or start w a number, or have spaces, etc
- bracket notation has fewer limitations. property identifiers can be variables
What are arrays used for?
arrays store a collection of elements of the same data type
Describe array literal notation.
var newArray = []
How are arrays different from “plain” objects?
- plain objects have properties
- arrays store lists of data of the same type
- arrays have a numeric key [a, b, c] is {0: a, 1: b, 2: c}
What number represents the first index of an array?
0
What is the length property of an array?
tells you how many elements are in the array
How do you calculate the last index of an array?
array.length-1
What is a function in JavaScript?
a block of code that does something or calculates something
Describe the parts of a function definition.
- the function keyword
- an optional name
- zero or more parameters
- a code block
- an optional return statement
function newFunction(parameter1, parameter2) { xyz action return xyz }
Describe the parts of a function call.
functionName(arguments)
When comparing them side-by-side, what are the differences between a function call and a function definition?
- function definition states what action the function will perform w its arguments
- function call invokes the function to perform the action
What is the difference between a parameter and an argument?
- parameter is a placeholder for where arguments will be applied
- argument is the actual value that will be going into the function
Why are function parameters useful?
- states all the information that will be needed to run the function
- allows you to reuse code with different data
What two effects does a return statement have on the behavior of a function?
- causes the function to produce a value
- exits the function
Why do we log things to the console?
console logging helps developers keep track of their code, making sure the outputs are correct
What is a method?
a function stored as a property of an object
How is a method different from any other function?
- a method acts on the data of the object it belongs to
- a function is standalone
How do you remove the last element from an array?
arrayName.pop()
How do you round a number down to the nearest integer?
Math.floor(number)
How do you generate a random number?
Math.random()
- multiply it by a number you want the range of
- the formula==>Math.floor(Math.random() * (end - start) +1) + start
How do you delete an element from an array?
arrayName.splice(start index, # of elements to be deleted, item1 to be added, item2, …)
-only the start index is required as an argument
How do you append an element to an array?
arrayName.push(newElement)
How do you break a string up into an array?
arrayName.split(‘’)
Do string methods change the original string? How would you check if you weren’t sure?
- strings are immutable
- string methods do not change the original string (they create new outputs)
- you can check by console logging the string value
Roughly how many string methods are there according to the MDN Web docs?
30-ish
Is the return value of a function or method useful in every situation?
no, sometimes the function or method is used just to perform an action
Roughly how many array methods are there according to the MDN Web docs?
36
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
How do you pre-pend an element to an array?
arrayName.unshift()
Give 6 examples of comparison operators.
==, !=, >, =, <=
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
sets a condition to help the computer decide which lines of code should be run next
Is ‘else’ required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if (condition) {
code to be run if condition is met
}
What are the three logical operators?
and &&
or ||
not !
How do you compare two different expressions in the same condition?
(expression1) &&/|| (expression2)
What is the purpose of a loop?
-run the same block of code a certain amount of time
What is the purpose of a condition expression in a loop?
lets the computer know when to stop the loop
What does “iteration” mean in the context of loops?
represents each instance of the code block in the curly brackets of the loop being run
When does the condition expression of a while loop get evaluated?
after every iteration
When does the initialization expression of a for loop get evaluated?
once, at the beginning
When does the condition expression of a for loop get evaluated?
the condition gets checked at the beginning of every iteration, following initialization
When does the final expression of a for loop get evaluated?
after the code block is run
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?
“for in” loop
when should you use a for loop vs a while loop vs a do while loop?
- for loop: need to run the code a specific number of times (condition is a counter)
- while loop: need to run the code an unknown number of times (condition is not a counter)
- do while loop: similar to a ‘while loop’ but will always run the statements inside curly brackets at least once, even if the condition is false
Why do we log things to the console?
for debugging purposes so that we know what our output is
What is a “model”?
- a representation of the actual thing
- in the DOM, a model is the data representation of all the objects that make up a web document
Which “document” is being referred to in the phrase Document Object Model?
-web document: the html page
What is the word “object” referring to in the phrase Document Object Model?
-javascript object
What is a DOM Tree?
the data representation of all the objects that make up a web document where there is a parent stem that branches into child branches