Sunday, September 19, 2021

ES6 (JavaScript) Arrow Functions explain in briefly


Arrow functions have newly introduced a way to write JavaScript code in a shorter way. in javascript, we can write a function in the following way


function myFunction(item){
	console.log(item);
}

we are able to write javascript function as following way



const myFunction = function(item){
	console.log(item);
}

in arrow function, we can write the following way


const myFunction = (item) => { 
	console.log(item);
}

If the function is returning something it can write the following way


The normal way of writing the function


function myFunction(item){
	return "Item is: " + item;
}

above code can write as follows


const myFunction = function(item){
	return "Item is: " + item;
}

The above code can write with arrow function


const myFunction = (item) => {return "Item is: " + item;}

if it’s a single line return keyword and brackets can be omitted as follows


const myFunction = item => "Item is: " + item;

See more details in the following link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions


Code snippets are added using highlight.js . The following blog explains how to add it to the blogger site. 


0 comments:

Post a Comment