
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain shorthand functions in JavaScript?
The arrow functions also known as shorthand functions were introduced in ES2015 and allows us to write function in a shorter way. They don’t have their own binding to this and get the this from the surrounding context.
Following is the code showing shorthand functions in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Shorthand function in JavaScript</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to invoke arrow and normal function</h3> <script> let resEle = document.querySelector(".result"); let objYear, obj1Year; let obj = { name: "rohan", age: 20, birthday() { function birthYear() { objYear = new Date().getFullYear() - this.age; } birthYear(); }, }; let obj1 = { name: "Shawn", age: 22, birthday() { let birthYear = () => { obj1Year = new Date().getFullYear() - this.age; }; birthYear(); }, }; document.querySelector(".Btn").addEventListener("click", () => { obj.birthday(); obj1.birthday(); resEle.innerHTML += " Without arrow function : Birth Year = " + objYear + " "; resEle.innerHTML += " With arrow function : Birth Year = " + obj1Year + " "; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Explain Generator functions in JavaScript?
- Explain higher order functions in JavaScript.
- Explain asynchronous functions in JavaScript with an example
- How to write shorthand for document.getElementById() method in JavaScript?
- Regular functions vs Arrow functions in JavaScript?
- Explain important functions in math.h library functions using C language
- Explain ValueFromPipeline in PowerShell Advanced Functions.
- Arrow functions in Javascript
- Explain C Error handling functions
- Explain str() vs repr() functions in Python
- Difference between regular functions and arrow functions in JavaScript
- Async/Await Functions in JavaScript
- What are functions in JavaScript?
- Using iterator functions in Javascript
- Fat arrow functions in JavaScript

Advertisements