DEV Community

Kiruthiga S
Kiruthiga S

Posted on

React JS

Arrow Function

without arrow function

function greet() {
    return "Hello";
}
Enter fullscreen mode Exit fullscreen mode

with arrow function

const greet = () => {
    return "Hello";
}
Enter fullscreen mode Exit fullscreen mode
const greet = () => "Hello";
Enter fullscreen mode Exit fullscreen mode

with parameters

function add(a,b){
    return a+b;
}
Enter fullscreen mode Exit fullscreen mode
const add = (a,b) => a+b;
Enter fullscreen mode Exit fullscreen mode

Why React uses Arrow Functions?

const Home = () => {
    return <h1>Home Page</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Destructuring
Taking values out of an object or array easily

Object Destructuring

const student = {
    name: "Alice",
    age: 22,
    city: "Chennai"
};
Enter fullscreen mode Exit fullscreen mode

without destructuring

console.log(student.name);
console.log(student.age);
Enter fullscreen mode Exit fullscreen mode

with destructuring

const { name, age } = student;

console.log(name);
console.log(age);
Enter fullscreen mode Exit fullscreen mode

Array Destructuring

const colors = ["Red","Blue","Green"];
Enter fullscreen mode Exit fullscreen mode

without destructuring

const first = colors[0];
const second = colors[1];
Enter fullscreen mode Exit fullscreen mode

with destructuring

const [first, second] = colors;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)