Q1. What is a function in JavaScript?
A function in JavaScript is a block of reusable code designed to perform a specific task.
Functions help avoid repeating the same code multiple times. They improve code readability, reusability, and organization.
Example
Functions help avoid repeating the same code multiple times. They improve code readability, reusability, and organization.
Example
function greet() { return "Hello World";}
console.log(greet());Q2. Why are functions important?
Functions are important because they help in organizing code, improving reusability, and reducing duplication.
They allow developers to break large programs into smaller, manageable parts.
They allow developers to break large programs into smaller, manageable parts.
Q3. How do you define a Function in JavaScript?
A function is defined using the function keyword followed by the function name, parentheses, and curly braces.
Example
Example
function add(a, b) { return a + b;}Q4. What is Function Declaration?
Function declaration is a method of creating a function using the function keyword.
Example
Function declarations are hoisted, meaning they can be used before they are declared.
Example
function sayHello() { console.log("Hello");}Function declarations are hoisted, meaning they can be used before they are declared.
Q5. What is Function Expression?
A function expression is when a function is assigned to a variable.
Example
Function expressions are not hoisted like function declarations.
Example
const multiply = function(a, b) { return a * b;};Function expressions are not hoisted like function declarations.
