JavaScript Functions


What is a function in JavaScript?

A function in JavaScript is a block of code designed to perform a specific task. Functions are reusable and can take parameters, return values, and be executed when called.


How do you declare a function in JavaScript?

You can declare a function using the function keyword followed by a name, parameters, and a block of code.


function greet(name) {
  return `Hello, ${name}!`;
}

Can you provide an example of a function declaration?


function add(a, b) {
  return a + b;
}

console.log(add(3, 4)); // 7

What is a function expression?

A function expression is a function that is assigned to a variable. It can be anonymous or named. Function expressions can be used as values and passed as arguments.


const multiply = function(x, y) {
  return x * y;
};

console.log(multiply(3, 5)); // 15

What is an arrow function?

An arrow function is a concise way to write function expressions using the arrow syntax (=>). Arrow functions do not have their own this, making them useful in certain contexts.


const divide = (a, b) => a / b;

console.log(divide(10, 2)); // 5

What are the benefits of using arrow functions?

Benefits of using arrow functions include:

  • Concise syntax, especially for short functions.
  • Lexical scoping of this, making it easier to use in callbacks.
  • Implicit return for single-expression functions.

What is the difference between a function declaration and a function expression?

The main differences are:

  • A function declaration is hoisted, meaning it can be called before its definition in the code.
  • A function expression is not hoisted, and you must define it before calling it.

// Function declaration
console.log(greet('Alice')); // Works
function greet(name) {
  return `Hello, ${name}!`;
}

// Function expression
console.log(square(5)); // TypeError: square is not a function
const square = function(x) {
  return x * x;
};

What is a return statement in a function?

The return statement is used to specify the value that a function should output when it is called. If no return statement is present, the function will return undefined.


function getGreeting() {
  return 'Hello!';
}
console.log(getGreeting()); // Hello!

How can you pass arguments to a function?

You can pass arguments to a function by specifying them in parentheses when calling the function. The values passed are assigned to the corresponding parameters in the function definition.


function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet('Alice')); // Hello, Alice!

What are default parameters in JavaScript functions?

Default parameters allow you to set default values for function parameters if no value or undefined is passed.


function multiply(a, b = 1) {
  return a * b;
}

console.log(multiply(5)); // 5 (uses default value for b)

What is the arguments object in JavaScript?

The arguments object is an array-like object available within all non-arrow functions, which contains the values of the arguments passed to the function. It is not available in arrow functions.


function showArgs() {
  console.log(arguments);
}

showArgs(1, 2, 3); // Outputs: [1, 2, 3]

What are rest parameters in JavaScript?

Rest parameters allow you to represent an indefinite number of arguments as an array. They are specified using three dots (...) before the parameter name.


function sum(...numbers) {
  return numbers.reduce((acc, curr) => acc + curr, 0);
}

console.log(sum(1, 2, 3, 4)); // 10

What are higher-order functions in JavaScript?

Higher-order functions are functions that can take other functions as arguments or return them as output. They enable functional programming patterns and callbacks.


function higherOrderFunction(callback) {
  callback();
}

higherOrderFunction(() => console.log('This is a callback function!'));

What is the purpose of the function expression in a variable?

Using a function expression in a variable allows you to define a function that can be invoked through the variable name, enabling you to pass functions as arguments, return them from other functions, and create closures.


const greet = function(name) {
  return `Hello, ${name}!`;
};

console.log(greet('Bob')); // Hello, Bob!

How can you create a function that returns another function?

You can create a function that returns another function, also known as a factory function. This is often used to create closures.


function createCounter() {
  let count = 0;
  return function() {
    count++;
    return count;
  };
}

const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
Ads