Techoceanhub logo

Arrow Functions

Arrow functions are a concise way to write JavaScript functions introduced in ES6 (ECMAScript 2015). They offer a shorter syntax compared to traditional function definitions. Here’s a breakdown of arrow functions with examples:

Simple Arrow Function:

This is the most basic form, where you have an argument and an implicit return statement (meaning the return keyword is not required).

const square = (x) => x * x;

console.log(square(5)); // Output: 25

Arrow Function with Multiple Parameters:

You can have multiple parameters just like regular functions.

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

console.log(add(3, 4)); // Output: 7
const isEven = (num) => {
  if (num % 2 === 0) {
    return true;
  } else {
    return false;
  }
};

console.log(isEven(10)); // Output: true

Arrow Functions as Callbacks:

Arrow functions are commonly used as callbacks in functions like map, filter, etc.

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((num) => num * 2);

console.log(doubled); // Output: [2, 4, 6, 8, 10]

Key points to remember:

  • Arrow functions are always anonymous (they don’t have a name).
  • They cannot be used as constructors.
  • Arrow functions have implicit this binding, which can be different from traditional functions.

I hope this explanation clarifies arrow functions!

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .