DEV Community

Pavithraarunachalam
Pavithraarunachalam

Posted on

Arrow function,Callback function,use Effect...

An arrow function is a shorter way to write a function in JavaScript.
It uses the => symbol instead of the function keyword.


Basic Syntax

const add = (a, b) => {
return a + b;
};

You can also write it even shorter if it's just one line:

const add = (a, b) => a + b;

Example:

Instead of writing:

function sayHello() {
  console.log("Hello");
}
Enter fullscreen mode Exit fullscreen mode

You can write:

const sayHello = () => {
  console.log("Hello");
};
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • It uses => instead of the function keyword.
  • It's shorter and cleaner.
  • It automatically uses the this from the place where it was written.

Callback Function:

A callback function in JavaScript is a function that is passed as an argument to another function, and it is called (or "called back") later.


Example:

function greet(name, callback) {
  console.log("Hello, " + name);
  callback();
}

function sayBye() {
  console.log("Goodbye!");
}

greet("John", sayBye);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, John
Goodbye!
Enter fullscreen mode Exit fullscreen mode

Here:

  • sayBye is a callback passed to greet.
  • greet calls sayBye() after greeting.

In Short:

A callback is:

  • A function passed into another function
  • Called later (after something happens)
  • Useful for things like waiting, repeating, or responding to events

UseEffect:

useEffect is a React Hook that lets you run side effects in function components.


useEffect runs after your component renders. You use it to do things like fetch data, update the DOM, or set up timers.


Basic Syntax:

import { useEffect } from 'react';

useEffect(() => {
  // Code to run (side effect)
});
Enter fullscreen mode Exit fullscreen mode


Important Notes:

  • useEffect runs after render.
  • You can control when it runs using the dependency array ([]).
  • You can return a function to do cleanup (like clearing timers or unsubscribing from listeners).

Top comments (0)