Quick Reference of Arrow Function in JavaScript
Arrow functions were introduced with ES6 as a new way of writing JavaScript functions. This post will cover basic syntax of Arrow functions, common use cases.Note – I am putting my JavaScript Developer study notes here. Note – Please read my previous post where I have explained the basics of functions in JavaScript.Basics of Functions in JavaScriptWhat is Arrow Functions?Arrow functions, sometimes referred as Fat Arrow is a new concise way of writing functions. They utilize a new syntax, => which looks like Fat Arrow. Arrow functions are anonymous and change the way this binds in functions. By using arrow functions, we can avoid typing function and return keyword and also curly brackets.Basic Syntax of Arrow Function with Multiple Parametersconst multiplyFunction = (x, y) => x * y;console.log(multiplyFunction(5, 6));Output: 30Syntax of Arrow Function with Single Parameterconst splitString = (text) => text.split(” “);console.log(splitString(“My name is Sudipta Deb”));Output: [ ‘My’, ‘name’, ‘is’, ‘Sudipta’, ‘Deb’ ]Syntax of Arrow Function with No Parameterconst functionWithNoParam = () => { console.log(“This function with no parameter”);};functionWithNoParam();Output: This function with no parameterSyntax of Arrow Function with Object returnArrow functions can be used to return an object literal expressions. var funcWithObject = (empName, empNumber) => ({ empName: empName, empNumber: empNumber,});console.log(funcWithObject(“Sudipta Deb”, 101));Output: { empName: ‘Sudipta Deb’, empNumber: 101 }Use Cases for Arrow FunctionsOne very common use case for Arrow function is Array manipulation. In the below example, each array items will be converted to upper case.const...
Read More