• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators○Type Conversion
○Conditionals○Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions●Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring○JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

Arrow Functions

Arrow functions are a concise syntax for writing functions introduced in ES6. They are especially popular for short one-liners and callbacks.

Syntax variations

javascript
// Traditional function expression
const add = function(a, b) { return a + b; };

// Arrow function — same result
const add = (a, b) => a + b;

// Single parameter — parentheses optional
const double = n => n * 2;

// No parameters — empty parentheses required
const greet = () => "Hello!";

// Multi-line body — needs {} and explicit return
const classify = n => {
    if (n > 0) return "positive";
    if (n < 0) return "negative";
    return "zero";
};

Key differences from regular functions

  • Arrow functions do not have their own this — they inherit this from the surrounding scope (important in OOP, covered later).
  • Arrow functions cannot be used as constructors.
  • Arrow functions do not have an arguments object.

Your Task

  1. Write an arrow function square(n) that returns n * n.
  2. Write an arrow function celsius(f) that converts Fahrenheit to Celsius: (f - 32) * 5/9, rounded to 1 decimal place (Math.round(value * 10) / 10).
  3. Write an arrow function isEven(n) that returns true if n is even, false otherwise.
  4. Display in #output: "5² = 25 | 98°F = 36.7°C | 4 is even: true".
Hint 1
1 / 3
HINT 1

Arrow function: const fn = (params) => expression;

Loading editor…
READY
intercourses
javascript