JavaScript Functions and Methods Flashcards
What is a function in JavaScript?
A function is a power tool that helps with reusing code throughout the program
It helps make code dynamic, code can be written once to handle more than one situation
Describe the parts of a function definition.
The function keyword to begin creating the function
A name for the function, which is optional
Zero or more parameters within parenthesis () separated by commas ,
The function code block within curly braces {}
Statements within the function code block, separated by semicolons ;
function funName (parameters) { functionCodeBlock with statements; };
Describe the parts of a function call.
You call the function by typing its name followed by zero or more arguments within parentheses followed by a semicolon
When comparing them side-by-side, what are the differences between a function call and a function definition?
The difference is the existence of code block and the swapping of parameters with arguments
What is the difference between a parameter and an argument?
A parameter is a variable with an undefined value until it is called with arguments
A parameter is a placeholder for arguments
Arguments are adding value to the parameter
Why are function parameters useful?
It is our way to provide data to the functions and it allows us to get different results
What two effects does a return statement have on the behavior of a function?
Caused the function to produce a value that can be used in the program
Prevents any more code in the functions code block from being run
Why do we log things to the console?
It is a powerful tool for debugging and it makes it easy to inspect variables in the browser
It helps the code communicate with us, we can observe data for ourselves
What is a method?
A method is a function, which is a property of an object
Instance Methods are built-in tasks performed by an object instance
Static Methods are tasks that are called directly on an object constructor
How is a method different from any other function?
A method is an object reference to a function
An Object Reference is a link to an object
Methods have more access to data and are related to objects
How do you remove the last element from an array?
You call the pop method of the array object
How do you round a number down to the nearest integer?
You call the floor method of the Math object with an argument of a numeric value
How do you generate a random number?
You call the random method of the Math object either with no arguments or arguments with numeric value
How do you delete an element from an array?
You call the splice method of the array object
How do you append an element to an array?
You call the push method of the array object