Q1. What is an Arrow Function
An Arrow Function is a shorter and cleaner way to write functions in JavaScript. It was introduced in ES6 (ECMAScript 2015).
Arrow functions use the => syntax and help reduce code length. They are commonly used in modern JavaScript applications.
Example
Arrow functions use the => syntax and help reduce code length. They are commonly used in modern JavaScript applications.
Example
const greet = () => { console.log("Hello World");};
greet();Q2. Can Arrow Functions have implicit return?
Yes, Arrow Functions can return values without using the return keyword when the function has a single expression.
Example
If there is only one expression, JavaScript automatically returns the result.
Example
const multiply = (a, b) => a * b;
console.log(multiply(4, 5));If there is only one expression, JavaScript automatically returns the result.
Q3. Can Arrow Functions have a single parameter without parentheses?
Yes, if there is only one parameter, parentheses can be removed.
Example
Example
const square = x => x * x;
console.log(square(6));Q4. Can Arrow Functions be used without parameters?
Yes, but empty parentheses are required.
Example
Example
const showMessage = () => { console.log("Welcome");};
showMessage();Q5. Why are Arrow Functions useful?
Arrow Functions provide:
- Shorter syntax
- Better readability
- Automatic return for single expressions
- Easier handling of this
