Coder Social home page Coder Social logo

arrow-functions-in-javascript's Introduction

Arrow-Functions

A brief understanding of arrow function in javascript

Introduction

Arrow functions are quite a beauty in javascript, which allows programers to take off the burden of defining the functions/methods with a function keyword.

Syntax

As the name defines this functions are defined by a symbol of arrow not this -> but this => "equal too followed by a greaterthan symbol"

 //arrow  functions without arguments.
 ()=>{ /*Statements*/ }
 
 //arrow functions with a single statement
 () => /*Statement*/
 
 //arrow functions with arguments
 (Argument1,Argument2,....) => { /*Statements*/ }
 
 //arrow function with a single argument and single statement can be written as
 Argument => /*Statement*/
 
 //arrow function with a single argument and many statements can be written as
 Argument => { /*Statements*/ }
 
 //arrow function while assiging a variable
 var (or) let (or) const variablename = (Arguments) =>{ /*Statements*/ }
 variablename();
 
 //a normal return to a arrow function
 ()=>{return(value);}
 
 //for a single statement with a return
 ()=>/*value (or) expression*/
 
 //sample
 addten= (a) => a+10
 addten(40) //this returns 50
 
 //more complex Example which return a arrow function is. 
 hello=()=>value=>value=>value
 hello()()("hello world")
 
 //Output Would be hello world

Usecase Example

//Without using a Arrow Key Word                                   
                                                              
var helloworld = function(){ console.log("Hello world"); }     
helloworld();                                                 
                                                              
//With using a Arrow function 

var helloworld = () => { console.log("Hello world"); }
helloworld();

Both the codes give the same output "Hello world", But you can see the difference in there syntax.

I feel it's a little cute and readable and easy to use, over a regular functions.

And for a single statement functions like the above code can be shortened to

//Arrow functions for a single statement functions

var helloworld => console.log("Hello World");
helloworld();

arrow-functions-in-javascript's People

Contributors

rohit-dasamantharao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.