What is a function in JavaScript?
A reusable block of code that performs a specific task.
How do you define a function?
Use the function keyword. Example: function greet() { console.log('Hello'); }
How do you call or invoke a function?
Use the function name followed by parentheses. Example: greet();
What are function parameters?
Variables listed in the function definition used to accept input values.
What are function arguments?
Actual values passed into a function when it’s called.
How do you return a value from a function?
Use the return keyword. Example: function add(a,b){ return a+b; }
What happens if no return statement is used?
The function returns undefined by default.
What is a function expression?
A function assigned to a variable. Example: const add = function(a,b){ return a+b; };
What is an arrow function?
A shorter syntax for writing functions. Example: const add = (a,b) => a+b;
How do arrow functions differ from regular functions?
Arrow functions don’t have their own this, arguments, or prototype.
What is a default parameter?
A value assigned to a parameter if no argument is provided. Example: function greet(name='Guest'){...}
What is the arguments object?
An array-like object containing all arguments passed to a function.
What is the difference between parameters and arguments?
Parameters are placeholders in the definition; arguments are actual values passed during execution.
What is a callback function?
A function passed as an argument to another function and executed later.
What is a higher-order function?
A function that takes another function as an argument or returns a function.
What is a pure function?
A function that always returns the same output for the same input and has no side effects.
What is a side effect in a function?
Any change outside the function’s scope, like modifying a global variable or DOM element.
What is recursion?
When a function calls itself until a condition is met.
What is function scope?
Variables declared inside a function are accessible only within that function.
What is lexical scope?
A function’s scope is determined by where it’s defined, not where it’s called.
What is a closure?
A function that retains access to variables from its outer scope even after the outer function has returned.
What is an IIFE (Immediately Invoked Function Expression)?
A function that runs as soon as it’s defined. Example: (function(){ console.log('Run'); })();
What is the this keyword in functions?
this refers to the object that owns the function, or the global object if none.
How can you bind this to a specific object?
Use .bind(), .call(), or .apply(). Example: func.call(obj);