ES6

Arrow Function in JavaScript – Square Root of Array Elements

📌 INTRODUCTION

Arrow functions are one of the most important features introduced in ES6 (ECMAScript 2015). They provide a shorter syntax and are widely used in modern JavaScript, especially in React and functional programming.

In this program, we will learn how to:

  • Use arrow functions
  • Work with arrays
  • Apply the map() function
  • Calculate square roots of elements

📘 PROBLEM STATEMENT

👉 Write a program using arrow function to find square root of elements of an array.


💡 CONCEPTS USED

  • Arrow Functions (=>)
  • Array methods (map())
  • Math functions (Math.sqrt())

🧠 LOGIC EXPLANATION

  1. Create an array of numbers
  2. Use map() to iterate over each element
  3. Apply Math.sqrt() to each element
  4. Store and display the result

💻 PROGRAM CODE

const numbers = [4, 9, 16, 25, 36];

// Arrow function with map()
const squareRoots = numbers.map(num => Math.sqrt(num));

console.log("Original Array:", numbers);
console.log("Square Roots:", squareRoots);

🖥️ OUTPUT

Original Array: [4, 9, 16, 25, 36]
Square Roots: [2, 3, 4, 5, 6]

🔍 STEP-BY-STEP EXPLANATION

  • numbers.map() → loops through each element
  • num => Math.sqrt(num) → arrow function calculates square root
  • Result is stored in squareRoots array

⚡ ALTERNATIVE METHOD (WITHOUT ARROW FUNCTION)

const numbers = [4, 9, 16, 25];

const result = numbers.map(function(num) {
return Math.sqrt(num);
});

console.log(result);

🎯 WHY ARROW FUNCTIONS ARE IMPORTANT

  • Shorter syntax
  • Cleaner and readable code
  • Widely used in React (hooks, components)
  • No need for function keyword

📚 REAL-WORLD USE CASE

  • Data transformation in APIs
  • Processing arrays in React apps
  • Handling numerical datasets

❓ VIVA QUESTIONS (IMPORTANT)

Q1. What is an arrow function?

👉 A concise way to write functions in JavaScript using =>.


Q2. What is map() used for?

👉 It transforms each element of an array and returns a new array.


Q3. Difference between arrow function and normal function?

👉 Arrow functions have shorter syntax and no this binding.


Q4. What does Math.sqrt() do?

👉 It returns the square root of a number.


Q5. Does map() modify original array?

👉 No, it returns a new array.


🔗 RELATED POSTS

👉 Complete MCA 168 Lab Programs List (link your main post here)


📌 CONCLUSION

This program helps you understand how arrow functions simplify JavaScript code and how powerful array methods like map() can be.

👉 Practice this program multiple times — it is very commonly asked in exams and interviews.


Further Reading

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

Java Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *