JavaScript Flashcards
What are objects used for?
Objects are used to group together a set of variables and functions.
What are object properties?
Object properties are just variables, but in objects.
What is object literal notation?
Object literal notation is an array of key + value pairs within a curly brace; a colon separates the keys and values and a comma separates every key + value pair.
How do you remove a property from an object?
To remove a property from an object, you use the delete operator.
Example: delete pet.name or delete pet[name].
What are functions in objects called?
Methods.
How would you read the below?
var student = { firstName: 'Luke', lastName: 'Skywalker', age: 25 };
“There is an object literal being assigned to the variable ‘student’ with the string ‘Luke’ being assigned to the firstName property, the string ‘Skywalker’ being assigned to the lastName property, and the number 25 being assigned to the age property.”
What are arrays used for?
Arrays are used to store lists of values.
How do you grab the first index of an array?
We use the number 0 since it represents the first index of an array.
Example: var chargersPlayers = ['Keenan', 'Herbert', 'Derwin', 'Bosa'] console.log(chargersPlayers[0]); //expected output: Keenan
What is the length property of an array?
It’s a property that’s used to return the number of elements within that array.
How do we calculate the last index of an array?
We subtract 1 from the length of the array.
Example:
var top5BasketballPlayers = [‘MJ’, ‘Kobe’, ‘LeBron’, ‘Steph’, ‘KD’];
var lastIndex = top5BasketballPlayers.length - 1;
console.log(top5BasketballPlayers[lastIndex]);
//expected output: KD
What is a function in JavaScript?
A JavaScript function is a block of code designed to perform a particular task.
What are the 6 parts of a function definition?
- function keyword
- name (optional)
- parameters (optional)
- start of the function’s code block
- return statement (optional)
- end of the function’s code block
How do you call a function?
To call a function, we use the parentheses following the function’s name.
Ex. getName( )
What is the difference between defining a function and calling a function?
When defining a function, we’re just creating the functionality of the function itself. Calling the function means that we’re actually running the function (with the parameters if there are any).
What is the difference between a parameter and an argument?
Function parameters are the names listed in the function’s definition. Function arguments are the real values passed to the function.
Why are function parameters useful?
Parameters are useful to functions, because otherwise you can’t give the function-machine an input.
What two effects does a return statement have on the behavior of a function?
- Causes the function to produce a value we can use in our program.
- Prevents any more code in the function’s code block from being run.
How should you go about naming your functions?
When it comes to naming functions, the name should summarize the code block. A good rule is to make sure your function’s name includes at least a verb. A verb-noun combination is best.
How would you read the following?
function convertMinutesToSeconds(minutes) { var seconds = minutes * 60; return seconds; }
“There is a function definition named convertMinutesToSeconds with a single parameter named minutes followed by an opening curly brace for the code block. The variable minutes is being multiplied by 60 and the result of that expression is being assigned to the variable seconds. The value of the variable is being return from the function.”
What is a expression in JavaScript?
An expression is a line of code that produces a value.
How would you read the following?
var convertMinutesToSecondsResult = convertMinutesToSeconds(5);
“The convertMinutesToSeconds is being called with one argument of 5 and the return of that function being assigned to the variable convertMinutesToSeconds.”
What is a method?
a function which is a property of an object.
How can you tell the difference between a method definition and a method call?
a method definition is usually within an object specifically assigned to a parameter whereas a method call has the object followed by a period/dot and the method name with optional parameters.