JavaScript Flashcards
What is the purpose of variables?
variables are used to store data/information to be used in scripts/functions
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
variableName = variableValue;
using the assignment operator
What characters are allowed in variable names?
Must start with letter, $, or _. Can contain letters, numbers, $’s, or _’s.
What does it mean to say that variable names are “case sensitive”?
name and Name are not the same variable
What is the purpose of a string?
Strings can be used for names, usernames, messages, etc. that need to be displayed.
What is the purpose of a number?
Numbers are used for mathematical functions, counting, positioning on a screen, etc.
What is the purpose of a boolean?
Booleans display values of true or false.
What does the = operator mean in JavaScript?
The = operator is used to assign a value to a variable.
How do you update the value of a variable?
By setting a new value to the variable name using the = operator.
What is the difference between null and undefined?
Null can be used as a placeholder value for a variable as it is still an object.
Undefined is when no value has been assigned to a variable yet.
Why is it a good habit to include “labels” when you log values to the browser console?
Labels make it more clear the reason that something is being logged to the console, or what the value being logged represents.
Helps with debugging.
Give five examples of JavaScript primitives.
String, number, boolean, null, and undefined.
What data type is returned by an arithmetic operation?
a number
What is string concatenation?
pushing two string values together into one string value
What purpose(s) does the + plus operator serve in JavaScript?
addition of numbers or concatenation of strings
What data type is returned by comparing two values (<, >, ===, etc)?
boolean values
What does the += “plus-equals” operator do?
adds a value on the right to the variable on the left, then assigns the new value to that same variable
What are objects used for?
Grouping together sets of variables and their values as well as methods, typically a set of traits describing a real-world object.
What are object properties?
variables stored within an object
Describe object literal notation.
variableName = {
property: value,
property: value
};
How do you remove a property from an object?
using the delete operator
delete objectName.objectProperty
What are the two ways to get or update the value of a property?
dot notation
objectName.objectProperty = propertyValue;
bracket notation
objectName[‘objectProperty’] = propertyValue;
What are arrays used for?
storing lists of values under a variable name
Describe array literal notation.
arrayName = [item1, item2, item3]
arrayName[1]
item2
How are arrays different from “plain” objects?
they store data in order, allowing the use of the length property to pull values
What number represents the first index of an array?
0
What is the length property of an array?
the length property is the number of values that are stored in an array
How do you calculate the last index of an array?
subtract 1 from the length of an array
arrayName.length - 1
What is a function in JavaScript?
a block of code that can be called upon by name to run multiple times, taking input and providing output.
Describe the parts of a function definition.
function functionName(parameters, if any) {
lines of code being run;
more lines of code being run;
}
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?
A function definition contains the block of code that will be running. A function call is just the name of the function and the arguments being taken, not any code being run.
What is the difference between a parameter and an argument?
A parameter is in the function definition, and is a placeholder for what is needed in order for the function to run as designed.
An argument is in the function call, and are the values needed to fulfill what the parameter is asking for.
Why are function parameters useful?
They are placeholders for information that will be passed as arguments in the function. When a function is called, the arguments will be directed to where all the placeholders are located within the function code to successfully run the function.
Makes the function more flexible, being able to be used for more than one type of output.
What two effects does a return statement have on the behavior of a function?
- It returns the value of a given variable to be used outside of the function block of code.
- It prevents any other code from being run in the function’s code block. (exits the function)
Why do we log things to the console?
To validate the expected results of a function
What is a method?
It is a function that is a property of an object
How is a method different from any other function?
They are specific to an object and must be called in reference to that 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
How do you generate a random number?
random method
How do you delete an element from an array?
pop, shift, or splice methods
How do you append an element to an array?
push or unshift methods
How do you break a string up into an array?
use the split method with commas
Do string methods change the original string? How would you check if you weren’t sure?
They do not change the original string, they create a new string. This is why the result must be stored in a new variable to be used
Roughly how many string methods are there according to the MDN Web docs?
60 - 70 methods (look up string on MDN; left column)
Is the return value of a function or method useful in every situation?
It is only useful when values need to be assigned to a variable from a function, or when a function needs to be terminated.