0% found this document useful (0 votes)
18 views

Arrow Function

The document describes arrow functions in JavaScript, which provide a concise syntax for writing functions. It explains the basic syntax of arrow functions using =>, provides examples of regular functions converted to arrow functions, and gives a practice problem to convert additional regular functions to arrow functions.

Uploaded by

janet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Arrow Function

The document describes arrow functions in JavaScript, which provide a concise syntax for writing functions. It explains the basic syntax of arrow functions using =>, provides examples of regular functions converted to arrow functions, and gives a practice problem to convert additional regular functions to arrow functions.

Uploaded by

janet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Arrow Function

Quick and Short way to write a function (=>)

Basic Syntax

const add = (a, b) => {

return a + b;

};

 (a, b) are the parameters the function takes.


 => is the arrow notation, which is like saying "becomes" or "returns."
 The code inside {} is the function body, where you put the logic of your function.
 The return a + b; line is the result that the function gives back.

Shorter

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

Examples

// Regular function

function sayHello(name) {

return "Hello, " + name + "!";

The sayHello function is a regular function. It takes a name parameter and returns a greeting string.
// Arrow function

const sayHello = (name) => "Hello, " + name + "!";

const sayHelloArrow = (name) => "Hello, " + name + "!";

// Using the functions

console.log(sayHello("Alice")); // Outputs: Hello, Alice!

console.log(sayHelloArrow("Bob")); // Outputs: Hello, Bob!

The sayHelloArrow function is an arrow function. It does the same thing as sayHello, but it's written
in a shorter way using the arrow (=>) syntax.

Nested Arrow Function

function outerFunction() {

const multiplyByTwo = x => x * 2;

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

const result1 = multiplyByTwo(5); // Result: 10

const result2 = square(3); // Result: 9

console.log(result1, result2);

outerFunction();

Difference will be seen only when a constructor and


keywords like “this” “super” are used in a program.
We will see that later.
Practice:
Convert the regular functions into arrow functions

1.Regular Function to Get the Square of a Number:

// Regular Function

function squareRegular(x) {

return x * x;

console.log(squareRegular(3)); // Output: 9

2.Regular Function to Check if a Number is Positive:

// Regular Function

function isPositiveRegular(number) {

return number > 0;

console.log(isPositiveRegular(5)); // Output: true

3.Regular Function to Concatenate Two Strings:

// Regular Function

function concatenateRegular(str1, str2) {

return str1 + ' ' + str2;

console.log(concatenateRegular('Hello', 'World!')); // Output: Hello World!

4.Regular Function to Calculate the Area of a Rectangle:

// Regular Function

function calculateAreaRegular(length, width) {

return length * width;

console.log(calculateAreaRegular(4, 5)); // Output: 20

5.Regular Function to Generate a Greeting:


// Regular Function

function greetRegular(name) {

return 'Hello, ' + name + '!';

console.log(greetRegular('Alice')); // Output: Hello, Alice!

6.Regular Function to Calculate Factorial:

// Regular Function

function factorialRegular(n) {

if (n === 0 || n === 1) {

return 1;

return n * factorialRegular(n - 1);

console.log(factorialRegular(5)); // Output: 120

7.Regular Function to Capitalize the First Letter of a String:

// Regular Function

function capitalizeRegular(str) {

return str.charAt(0).toUpperCase() + str.slice(1);

console.log(capitalizeRegular('hello')); // Output: Hello

You might also like