JavaScript Flashcards
What is the purpose of variables?
Variables are used to represent values/store information that can be used in your code.
How do you declare a variable?
by using the keyword var followed by a name to represent your variable.
How do you initialize (assign a value to) a variable?
By using the assignment operator followed by a value
What characters are allowed in variable names?
letters, numbers, dollar signs, or underscores. However, the name can NOT start with a number
What does it mean to say that variable names are “case sensitive”?
variables names are sensitive to the capitalization of letters. Example: var keyword and var keyWord are two different variables.
What is the purpose of a string?
a string is a data. type that is represented using characters and text.
What is the purpose of a number?
a number is a data type represented by numbers and is used for calculating values. Used to store numeric values.
What is the purpose of a boolean?
a boolean is a data type that only has two values: true and false. Good for logical reasoning
What does the = operator mean in JavaScript?
assignment operator. assigns values to avaraibles
How do you update the value of a variable?
by taking the variable name and assigning it a new value with he assignment operator, So long as this line of code comes after the initial value assignment.
What is the difference between null and undefined?
null is intentionally assigned an empty value and its type is an object, whereas undefined can be unintentional and means a variable has been declared but not assigned a value yet.
Why is it a good habit to include “labels” when you log values to the browser console?
So you know what variable/information you are logging to the console.
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 strings together into one variable using + operator
What purpose(s) does the + plus operator serve in JavaScript?
addition or concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds a value to the variables original value.
What are objects used for?
objects are used for storing multiple values.
What are object properties?
object properties are variables that are stored within an object.
Describe object literal notation.
object literal notation involves a set of curly braces holding a comma separated list of properties and methods to which is being stored in a variable.
How do you remove a property from an object?
by using the delete operator(keyword) on the object
What are the two ways to get or update the value of a property?
dot notation and bracket notation.
What are arrays used for?
For storing lists of data
Describe array literal notation.
brackets with data types separated by commas. All assigned into a variable.
How are arrays different from “plain” objects?
arrays have index numbers whereas objects have property names
What number represents the first index of an array?
0
What is the length property of an array?
the length property gives you the number of items in an array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
a javascript function is an object that can be called to perform a task.
Describe the parts of a function definition.
function keyword, optional name, a comma separated list of at least 0 parameters wrapped in ( ), and definition block.
Describe the parts of a function call.
function name followed by parenthesis containing any arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
the function definition includes the function keyword, function name, parameters, and definition block.
the function call only has the function name and arguments.
What is the difference between a parameter and an argument?
parameters are for definitions whereas arguments are passed when the function is called.
Why are function parameters useful?
parameters are placeholders for data that will be collected when the function is called.
What two effects does a return statement have on the behavior of a function?
returns a value from the definition block and ends the function.
Why do we log things to the console?
to ensure our code is producing the right outputs. To check our work. Too visualize what the code is doing and identify mistakes if any.
What is a method?
A function that is being applied to an object
How is a method different from any other function?
Methods are associated with objects and have special access to information stored in its object
How do you remove the last element from an array?
pop method
How do you round a number down to the nearest integer?
floor method of the Math object
How do you generate a random number?
random method of the Math object
How do you delete an element from an array?
pop, shift, or splice method
How do you append an element to an array?
push method
How do you break a string up into an array?
split method
Do string methods change the original string? How would you check if you weren’t sure?
No. log variables to the console to check.
Is the return value of a function or method useful in every situation?
no.
Give 6 examples of comparison operators.
less than, greater than, less than or equal to, greater than or equal to, not equal to, strictly equal to.
What data type do comparison expressions evaluate to?
booleans
What is the purpose of an if statement?
to check if a given condition is true or false, and if so, run code based on the condition.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, condition statement, code block
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
by wrapping the expressions in parenthesis
What is the purpose of a loop?
to repeat a piece of code a number of times
What is the purpose of a condition expression in a loop?
A condition allows for a piece of code to run repeatedly until the condition evaluates as false, then the loop will stop.
What does “iteration” mean in the context of loops?
An iteration is a single execution of a code block in a loop.
When does the condition expression of a while loop get evaluated?
Before each execution of its code block.
When does the initialization expression of a for loop get evaluated?
The initialization gets evaluated once at the very beginning of the loop. Also comes before the condition.
When does the condition expression of a for loop get evaluated?
right before each loop iteration.
When does the final expression of a for loop get evaluated?
at the end of each loop iteration.
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?
increase by 1
How do you iterate through the keys of an object?
for…in loop
Why do we log things to the console?
For debugging and checking values.
What is a “model”?
It is a representation of something.
Which “document” is being referred to in the phrase Document Object Model?
The html document being displayed by the browser.
What is the word “object” referring to in the phrase Document Object Model?
Object refers to all the objects that make up the document.
What is a DOM Tree?
The DOM tree is the structure in which the browser creates a model of the document,
Give two examples of document methods that retrieve a single element from the DOM.
.querySelector() and .getElementById()
Give one example of a document method that retrieves multiple elements from the DOM at once.
.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
For reusability.
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 content instead of at the top?
So the DOM has time to load before running any javascript.
What does document.querySelector() take as its argument and what does it return?
it takes a css selector in the form of a string and returns the FIRST element matching the selector.
What does document.querySelectorAll() take as its argument and what does it return?
it takes a css selector in the form of a string and returns ALL elements(node list) matching the selector.
What is the purpose of events and event handling?
Events and event handling allow for a web page to respond to a user interaction
What do [] square brackets mean in function and method syntax documentation?
They indicate that a parameter is optional
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener(‘event’, functionName);
What is a callback function?
A call back function is a function that is passed into another function as an argument.
What object is passed into an event listener callback when the event fires?
The Event Object.
What is the event.target?
the target property of the event object is a reference to the element that the event was dispatched on.
What is the className property of element objects?
The className property is a property that allows you to access and set a value for the class name attribute of an element.
How do you update the CSS class attribute of an element using JavaScript?
With className property.