JAVASCRIPT FUNCTIONS EXAM
QUESTIONS AND ANSWERS ALL
CORRECT
What is the arity of a function? - Answer-the number of parameters it declares in its
parameter list (read: the number of arguments it expects)
When does a function inherit properties from the prototype object? - Answer-When it is
used as a constructor
What do apply() and call() allow you to do? - Answer-Indirectly invoke a function as if it
were a method of some other object
When is a function recursive? - Answer-A function is recursive if it directly or indirectly
invokes itself
What is a closure? - Answer-A closure is an inner function that has access to the outer
(enclosing) function's variables—scope chain
What three scope chains do closures have access to? - Answer-The closure has three
scope chains: it has access to its own scope (variables defined between its curly
brackets), it has access to the outer function's variables, and it has access to the global
variables.
What is hoisting? - Answer-All variable declarations are hoisted (lifted and declared) to
the top of the function, if defined in a function, or the top of the global context, if outside
a function.
In what order does function declaration, variable declaration or variable assignment take
precedence? - Answer-function declaration takes precedence over variable declarations
(but not over variable assignment).
What is a function? - Answer-A function is a block of Javascript code that is defined
once, but can be executed multiple times.
, What is a function expression? - Answer-it is a variable with a value of a function.
What are parameters? - Answer-A list of identifiers that act as local variables within the
body of the function.
What is a constructor? - Answer-A constructor is a function used for initializing new
objects, and you use the new keyword to call the constructor.
When is a function known as a method? - Answer-When it is assigned to the property of
an object.
When are function names optional? - Answer-For function definition expressions, where
the name refers only to the function object within the body of the function. In essence,
the function name becomes a local variable used within the function. Whereas a
function declaration declares a variable and assigns a function object to it.
What is scope? - Answer-The context in which a variable exists.
What happens if you initialize a variable, but don't declare it inside a local function? -
Answer-It becomes a global variable.
Do local or global variables have priority? - Answer-If you declare a global variable and
a local variable with the same name, the local variable will have priority when you
attempt to use the variable inside a function because the search for a variable with that
name begins inside of the function and moves outward.
What object are all global variables attached to? - Answer-The window object
What happens when you put a function definition inside a conditional (if) block or a
loop? - Answer-Well, don't do that. Different JavaScript platforms in different browsers
have traditionally done different things in that situation, and the latest standard actually
forbids it. If you want your programs to behave consistently, only use this form of
function-defining statements in the outermost block of a function or program.
function example() {
function a() {} // Okay
if (something) {
function b() {} // Danger!
}
}
When is a function the invocation context for an object? - Answer-When a function is
invoked on or through an object
Are function expressions hoisted? If so, can they be invoked anywhere in the code? -
Answer-No, variable declarations are hoisted, but assignments to those variables are
not, so functions defined with expressions cannot be invoked before they are assigned.
QUESTIONS AND ANSWERS ALL
CORRECT
What is the arity of a function? - Answer-the number of parameters it declares in its
parameter list (read: the number of arguments it expects)
When does a function inherit properties from the prototype object? - Answer-When it is
used as a constructor
What do apply() and call() allow you to do? - Answer-Indirectly invoke a function as if it
were a method of some other object
When is a function recursive? - Answer-A function is recursive if it directly or indirectly
invokes itself
What is a closure? - Answer-A closure is an inner function that has access to the outer
(enclosing) function's variables—scope chain
What three scope chains do closures have access to? - Answer-The closure has three
scope chains: it has access to its own scope (variables defined between its curly
brackets), it has access to the outer function's variables, and it has access to the global
variables.
What is hoisting? - Answer-All variable declarations are hoisted (lifted and declared) to
the top of the function, if defined in a function, or the top of the global context, if outside
a function.
In what order does function declaration, variable declaration or variable assignment take
precedence? - Answer-function declaration takes precedence over variable declarations
(but not over variable assignment).
What is a function? - Answer-A function is a block of Javascript code that is defined
once, but can be executed multiple times.
, What is a function expression? - Answer-it is a variable with a value of a function.
What are parameters? - Answer-A list of identifiers that act as local variables within the
body of the function.
What is a constructor? - Answer-A constructor is a function used for initializing new
objects, and you use the new keyword to call the constructor.
When is a function known as a method? - Answer-When it is assigned to the property of
an object.
When are function names optional? - Answer-For function definition expressions, where
the name refers only to the function object within the body of the function. In essence,
the function name becomes a local variable used within the function. Whereas a
function declaration declares a variable and assigns a function object to it.
What is scope? - Answer-The context in which a variable exists.
What happens if you initialize a variable, but don't declare it inside a local function? -
Answer-It becomes a global variable.
Do local or global variables have priority? - Answer-If you declare a global variable and
a local variable with the same name, the local variable will have priority when you
attempt to use the variable inside a function because the search for a variable with that
name begins inside of the function and moves outward.
What object are all global variables attached to? - Answer-The window object
What happens when you put a function definition inside a conditional (if) block or a
loop? - Answer-Well, don't do that. Different JavaScript platforms in different browsers
have traditionally done different things in that situation, and the latest standard actually
forbids it. If you want your programs to behave consistently, only use this form of
function-defining statements in the outermost block of a function or program.
function example() {
function a() {} // Okay
if (something) {
function b() {} // Danger!
}
}
When is a function the invocation context for an object? - Answer-When a function is
invoked on or through an object
Are function expressions hoisted? If so, can they be invoked anywhere in the code? -
Answer-No, variable declarations are hoisted, but assignments to those variables are
not, so functions defined with expressions cannot be invoked before they are assigned.