Functions Flashcards
What is the invocation context of a function ?
It is the value of the ‘this’ keyword.
Usually, when a function that is a property of an object is invoked on (or through) this object, that object is the invocation context or the ‘this’ value for the function.
Javascript functions are closures. What does it mean ?
It mean that javascript functions can be nested within other functions, and they have access to any variables that are in scope where there are defined.
What are the different ways to create functions in javascript ?
- Using the function keyword either as a declaration or as an expression
- Using the shorthand syntax (arrow functions)
- In object litterals and class definitions, using the shorthand syntax for declaring methods. As well as getters and setters.
- Using the Function constructor
- Using specialized kind of functions as : generators (function*) and async functions.
What does the following mean: Function declarations are hoisted ?
Function declarations are hoisted to the top of the enclosing script, function or block so that functions defined in this way may be invoked from code that appear before the definition.
It means that all of the function declared in a block of javascript code will be defined throughout that block, and they will be defined before the javascript interpreter begins to execute any of the code in that block.
Functions are objects.
True or False ?
True
What is a function in JavaScript?
A block of JavaScript code that is defined once but may be executed any number of times.
What are parameters in a JavaScript function?
Identifiers that work as local variables for the body of the function.
What are arguments in a function invocation?
Values provided to a function’s parameters during invocation.
What is the return value of a function?
The value computed by the function using its argument values.
What does the ‘this’ keyword represent in a function invocation?
The invocation context of the function.
What is a method in JavaScript?
A function assigned to a property of an object.
What are constructors in JavaScript?
Functions designed to initialize a newly created object.
How are JavaScript functions treated in the language?
JavaScript functions are objects and can be manipulated by programs.
What is a closure in JavaScript?
A function that has access to its own scope, the outer function’s scope, and the global scope.
What is the most straightforward way to define a JavaScript function?
Using the function keyword.
What are arrow functions in JavaScript?
A compact syntax for defining functions introduced in ES6, using the => syntax.
What is the syntax for a function declaration?
function keyword, followed by an identifier, parentheses with parameters, and curly braces with statements.
What is hoisting in JavaScript functions?
Function declaration statements are moved to the top of their enclosing block, allowing them to be invoked before their definition.
What happens if a function does not contain a return statement?
It executes until the end and returns undefined.
True or False: In ES6, function declarations are only allowed at the top level.
False.
What is the difference between function declarations and function expressions?
Declarations are hoisted and can be called before their definition; expressions are not hoisted.
Fill in the blank: Arrow functions use the _______ syntax to separate parameters from the function body.
=>
What must be included when returning an object literal from an arrow function?
The object literal must be inside parentheses.
What is the significance of the ‘this’ keyword in arrow functions?
Arrow functions inherit the value of ‘this’ from the surrounding context.