JavaScript Flashcards
What is the purpose of variables?
Lets us store values and be reusable
How do you declare a variable?
Use keyword var
How do you initialize (assign a value to) a variable?
Use variable name and assignment operator
What characters are allowed in variable names?
Letters, numbers, underscore, dollar sign (cant start with number)
What does it mean to say that variable names are ‘case sensitive’?
Two variable names that are the same letters but with different capitalization
What is the purpose of a string?
Stores text values
What is the purpose of a number?
Stores numeric values
What is the purpose of a boolean?
Stores TRUE or FALSE. Allows for logic and decisions
What does the = operator mean in JavaScript?
The assignment operator
How do you update the value of a variable?
Assign it to a value again, no keyword at the start
What is the difference between null and undefined?
Null is intentionally absent of a value, also an object
Undefined is lack of value since nothing has been done there
Why is it a good habit to include “labels” when you log values to the browser console?
Helps with debugging and context in what we will see in the console
Give 5 examples of JavaScript primitives.
- String
- Boolean
- Number
- Undefined
- Null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Using a + to add two or more strings together
What purpose(s) does the + plus operator serve in JavaScript?
It can either add numbers together or concatenate strings into one string
What data type is returned by comparing 2 values ( <, >, ===, etc.)?
Boolean
What does the += “plus-equals” operator do?
It concatenates the strings and returns the expression as a string
What are objects used for?
Objects group together related variables and functions into a single object
What are object properties?
Properties are like variables that are part of an object. They store values like numbers, strings, booleans, arrays, and objects
Describe object literal notation.
A way to create an object
Start with curly braces, then property/method that is paired with a value/function. Use commas to separate those key/value pairs
How do you remove a property from an object?
Use the delete operator
What are the two ways to get or update the value of a property?
Dot notation or bracket notation
What are arrays used for?
Stores list-like info
Describe array literal notation.
Square brackets with values inside being delineated by commas
How are arrays different from “plain” objects?
- The keys are incrementing numbers (indexes)
- Objects can do dot notation
- Objects don’t have order
- Empty arrays have a method built-in already, “length”
- Adding to objects is different from adding to arrays
What number represents the first index of an array?
0
What is the length property of an array?
The number of items in the array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
Procedures. It packages code up into a reusable form.
Describe the parts of a function definition.
Function -name of function0 (parameter 1, 2, etc.) {
code block;
optional return value;
}
Describe the parts of a function call.
Name(argument 1, 2,…)
When comparing them side-by-side, what are the differences between a function call and a function definition?
Calls do not have the keyword function
Definitions have a code block
What is the difference between a parameter and an argument?
Parameters are part of the function definition
Arguments are the actual values that get passed in
Why are function parameters useful?
They make the code within the function reusable for multiple variables
What 2 effects does a return statement have on the behavior of a function?
Finds output value of function
Exits the function
Why do we log things to the console?
Helps with debugging and see what are code is doing and working
What is a method?
A function with is a property of an object
How do you remove the last element of an array?
Pop method
How is a method different from any other function?
They are specific to the object that they belong to and must be called with reference to 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?
Math.floor method
How do you generate a random number?
Math.random() generates a pseudo-random number between 0 and 1
How do you delete an element from an array?
Splice method uses 2 arguments (startIndex, numOfItemsToRemove)
Pop method removes from the end
Shift method deletes from the beginning
How do you append an element to an array?
Push method adds to the end
Splice method adds to a specific location
Unshift method adds to the beginning
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. We can check by using some string methods on a string and log the value of the string to see if it had undergone any transformations
Give 6 examples of comparison operators.
- ===
- !==
- >
- > =
- <
- <=
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Allows our programs to make decisions
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (expression that evaluates to boolean) { code block } else { code block }
What are the 3 logical operators?
- && (logical and)
- || (logical or)
- ! (logical not)
How do you compare two different expressions in the same condition?
Wrap each expression in parentheses, then use a comparison operator between them to have the entire expression evaluate down to a single boolean
What is the purpose of a loop?
Do a block of code repeated number of times
What is the purpose of a condition expression in a loop?
To check if another loop should be executed
What does iteration mean in the context of loops?
One execution of the code block
When does the condition expression of a while loop get evaluated?
At the beginning of each iteration, including the first
When does the initialization expression of a for loop get evaluated?
At the beginning of the loop (just once)
When does the condition expression of a for loop get evaluated?
At the beginning of each iteration, including the first
When does the final expression of a for loop get evaluated?
At the end of each 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?
Increments the value by 1 and returns the current value
How do you iterate through the keys of an object?
Use a for..in loop
What is a ‘model’?
Something that represents something
Which ‘document’ is being referred to in the phrase Document Object Model?
The web page as a whole