
Arrow Functions in JavaScript: A Simpler Way to Write Functions
Arrow function allow shorter syntax to write function in JavaScript They were introduced in ES6 to improve code readability Before arrow function: function greet(name){ return "hello" + name } After Arrow function const greet = (name)=>{return "hello" +name} const greet = name=> "hello" + name Syntax break down const functionName = (parameters) => { // code }; `- const → usually used to define arrow functions (parameters) → inputs => → arrow symbol {} → function body` Arrow function with one parameter const greet = name => "hello" + name if the function has only one statement that returns a value.You can remove the brackets and return value Arrow function with mutltiple parameter const greet = (firstname, lastname) => "hello" + firstname + lastname Arrow function with multiple parameter and multiple statement const greet = (firstname, lastname) => { let name = firstname + lastname return "hello" + name } with mutliple statement it is important to have bracket and return statement Arrow
Continue reading on Dev.to Webdev
Opens in a new tab



