Podcast
Questions and Answers
What value will be logged after executing myFunction()
? Assume let a; let b;
are appropriately declared.
What will be logged when console.log(b);
is executed after calling myFunction()
that assigns b = true;
?
What does myFunction(5, 10);
log to the console?
What would be the output of console.log(myFunction(5, 10));
?
Signup and view all the answers
What naming convention is recommended for functions that create something?
Signup and view all the answers
What components are essential in a function declaration?
Signup and view all the answers
What will the following code return: function square(number) { return number * number; }
when called with square(4)
?
Signup and view all the answers
How is a function expression different from a function declaration?
Signup and view all the answers
What will happen if you try to call the function subtract
before it is defined as a function expression?
Signup and view all the answers
Which statement correctly describes local variables in a function?
Signup and view all the answers
What is displayed by console.log(value)
if let value = square(3);
where square
is defined as function square(num) { let result = num * num; return result; }
?
Signup and view all the answers
What defines the scope chain in JavaScript functions?
Signup and view all the answers
What will occur if you try to return a non-existing parameter from a function?
Signup and view all the answers
What will be the output of alert(message) when called inside showMessage() if userName is 'Tom'?
Signup and view all the answers
What is the value of userName after calling showMessage() when userName is initially 'John'?
Signup and view all the answers
In the context of scope chain, what happens if a variable is not found in the current scope?
Signup and view all the answers
If a variable a is declared in both global and function scope, which value will console.log(a) display inside the function if a = 15 is assigned?
Signup and view all the answers
What is the order of variable resolution when accessing a variable in a nested function?
Signup and view all the answers
What will the first console.log(a) output if a is declared in the global scope and is then redeclared in myFunction() without initializing it?
Signup and view all the answers
What is the result of attempting to alert(message) if the variable message is defined locally in showMessage()?
Signup and view all the answers
When a function modifies an outer variable, what does this imply about the variable's access?
Signup and view all the answers
What is the output of the function 'test' when called with parameters 'a' and 'b' as defined?
Signup and view all the answers
What is the difference between a function declaration and a function expression?
Signup and view all the answers
What does the 'setTimeout' method do in JavaScript?
Signup and view all the answers
What will be printed as the value of 'i' during the execution of 'setInterval' in the second example?
Signup and view all the answers
Which of the following statements about callback functions is true?
Signup and view all the answers
What will be the output of the following code: 'console.log(greet(personName, personCity));'?
Signup and view all the answers
How does functional programming differ from imperative programming?
Signup and view all the answers
Which of the following can be a valid argument for 'setTimeout'?
Signup and view all the answers
What will be the output of the following code: let scoreText1 = getScoreText();
?
Signup and view all the answers
What does the rest parameter syntax (...theArgs
) allow a function to do?
Signup and view all the answers
Which of the following correctly defines an arrow function with two parameters?
Signup and view all the answers
What does the keyword 'const' signify in JavaScript?
Signup and view all the answers
Which of the following statements about the var
keyword is true?
Signup and view all the answers
In the function getScoreText(undefined, 99)
, what value will be assigned to the name
parameter?
Signup and view all the answers
Which of the following is NOT a valid way to define an arrow function?
Signup and view all the answers
What will be the value of a
inside the function myF
after executing var a = 10;
?
Signup and view all the answers
Study Notes
Functions
- Functions are blocks of code designed to perform a specific task.
- Function declaration: Defines a function using the
function
keyword, followed by the function name, parameters, and code block. - Function expression: Creates a function and assigns it to a variable.
- Local variables: Variables declared inside a function are only accessible within that function.
- Scope chain: JavaScript engine searches for a variable's value within the current scope. If not found, it searches in outer scopes.
- Naming a function: Use descriptive names (e.g.,
calculateSum
,getDetails
). - Default arguments: Assign default values to function parameters if no argument is provided (e.g.,
function greet(name = "Guest")
). - Arrow function expression: A concise way to create functions (e.g.,
const multiply = (x, y) => x * y
). - Callback functions: Functions passed as arguments to other functions or returned as values.
- Rest parameters: Accepts an indefinite number of arguments as an array.
- Hoisting: Moving function and variable declarations to the top of their current scope.
Local Variables
- A variable declared within a function is only accessible inside that function's scope.
Outer Variables
- A function can access outer variables, but it's not recommended to modify them directly.
Scope Chain
- JavaScript searches for a variable's value within the current scope. If not found, it checks other scopes until it finds the variable or reaches the global scope.
Variables
-
var
: Declares a variable with function scope. -
let
: Declares a block-scope local variable, not accessible outside the block it is defined in. -
const
: Declares a constant (read-only) variable with block scope.
Global vs. Local Scope
- Global variables are accessible throughout the program but should be avoided when possible. Local variables are accessible only within the block they are declared in.
Hoisting
- JavaScript moves variable and function declarations to the top of the current scope. This may lead to unexpected results if the declared variable or function has been referenced earlier.
First-Class and Higher-Order Functions
- First-class functions: Functions can be assigned to variables, passed to other functions and returned as values.
- Higher-order functions: Functions that take functions as arguments, or return functions as values.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Dive into the essential concepts of functions in JavaScript. This quiz covers function declarations, expressions, local variables, scope chain, and more. Test your knowledge on naming conventions, default arguments, arrow functions, callback functions, and rest parameters.