JavaScript Flashcards
What is the purpose of variables?
To store/see data that came together from the past & preserve data to use in the future
How do you declare a variable?
By using let, const or var
How do you initialize (assign a value to) a variable?
By using the assignment operator to give it value
What characters are allowed in variable names?
Letter
Underscore
$
numbers (but it cannot be the first character)
What does it mean to say that variable names are “case sensitive”?
Capitalized and lowercase letters mean different things
What is the purpose of a string?
To store a series of characters
What is the purpose of a number?
To perform math
To represent numeric values
What is the purpose of a boolean?
To make decisions
What does the = operator mean in JavaScript?
To make it hold/contain a value
How do you update the value of a variable?
By assigning a new value
What is the difference between null and undefined?
Null is intentional absence/emptiness
Undefined means there is no value assigned
Why is it a good habit to include “labels” when you log values to the browser console?
To make it easy to know which value the console is printing
Give five examples of JavaScript primitives.
Boolean Null Number String Undefined
(and BigInt and Symbol)
What data type is returned by an arithmetic operation?
Numeric/number
What is string concatenation?
To join/append strings
What purpose(s) does the + plus operator serve in JavaScript?
To add numbers or to concatenate strings
What data type is returned by comparing two values (x === x, etc)?
Boolean
What does the += “plus-equals” operator do?
Adds the value on the right side of the operator to the variable and assigns the result back to the variable
What are objects used for?
To group together a set of properties and methods
What are object properties?
Chunk of data attached to objects
Describe object literal notation
var variableName = {
key : value,
key : value
}
How do you remove a property from an object?
With the delete operator
What are the two ways to get or update the value of a property?
By using dot notation or square brackets
What are arrays used for?
For storing a list of values
Describe array literal notation
arrayname[‘value’, ‘value’];
How are arrays different from “plain” objects?
There is an order
They are numerically indexed
It keeps a count of the number of values in it
What number represents the first index of an array?
0
What is the length property of an array?
It’s a property that returns the number of values in that array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
Reusable block of code
Describe the parts of a function definition.
function optName(parameters) { block of code to run optional return keyword };
Describe the parts of a function call:
functionName( );
When comparing them side-by-side, what are the differences between:
a function call
vs
a function definition?
- No function keyword
- No curly braces and code block
What is the difference between a parameter and an argument?
Parameter is a placeholder name
Argument is the value that actually gets passed
Why are function parameters useful?
They give us an idea of what kind of value were looking to put in the argument
What two effects does a return statement have on the behavior of a function?
- Produces a value
2. Ends the code block from running
Why do we log things to the console?
- To see change over time
- As a debugging mechanism
What is a method?
Function stored in a property
function that is the property of an object
How is a method different from any other function?
They are attached to objects (so you have to specify the object name)
How do you remove the last element from an array?
.pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array?
.splice(index, how many items to remove)
How do you append (add to end) an element to an array?
.push()
How do you break a string up into an array?
.split()
Do string methods change the original string?
How would you check if you weren’t sure?
No (strings are immutable)
console.log()
Is the return value of a function or method useful in every situation?
No because sometimes you’re interested in simply what the function/method does and not its return value
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
== === != !== < ><= >=
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Make a decision on which code block to run depending on the Boolean result
Is else required in order to use an if statement?
No – you don’t always need an alternative
Describe the syntax (structure) of an if statement.
if (condition) { statement1; } else { statement2; }
What are the three logical operators?
&& (and)
|| (or)
! (not)
How do you compare two different expressions in the same condition?
By using logical operators && or ||
What is the purpose of a loop?
To keep executing a code as long as a condition runs true
What is the purpose of a condition expression in a loop?
To let the function know when to end
What does “iteration” mean in the context of loops?
One execution of code block
When does the condition expression of a while loop get evaluated?
In the beginning before the code block executes
When does the initialization expression of a for loop get evaluated?
In the beginning before anything
When does the condition expression of a for loop get evaluated?
Before each iteration
When does the final expression of a for loop get evaluated?
After the code block is executed
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 value
How do you iterate through the keys of an object?
for…in loop
What must the return value of myFunction be if the following expression is possible?
myFunction()();
A function!
myFunction is being called and the return is being called immediately
What does this code do?
const wrap = value => () => value;
Defines a function that defines the value and defines another function that returns that value
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
Defined
What allows JavaScript functions to “remember” values from their surroundings?
Closures
What does fetch() return?
A Promise
If you get back a promise what do you do with it? You can call .then OR .catch on it
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
fetch(‘string of resource’, { method: ‘GET’ })