Learn about function declarations, expressions, and arrow functions in JavaScript.
this
keyword.
Example showing different ways to define functions:
// Function Declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Function Expression
const greetExpr = function(name) {
return `Hi, ${name}!`;
};
// Arrow Function
const greetArrow = (name) => `Hey, ${name}!`;
// Usage
console.log(greet("Alice"));
console.log(greetExpr("Bob"));
console.log(greetArrow("Charlie"));
This code demonstrates:
this
binding.Q1: Which type of function is hoisted in JavaScript?
Answer: A. Function Declaration
Q2: Arrow functions differ from regular functions mainly because:
this
Answer: A. They do not have their own this
Q3: Which syntax is a function expression?
function foo() {}
const foo = function() {}
const foo = () => {}
Answer: D. Both B and C
Q4: What will the following arrow function return? () => 5 + 3
Answer: B. A function