06.07.2017 Views

Mastering JavaScript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7<br />

The body of the function can be enclosed by { .. } if there's more than one<br />

expression in the body. If there's only one expression, and you omit the surrounding<br />

{ .. }, there's an implied return in front of the expression. There are several<br />

variations of how you can write arrow functions. The following are the most<br />

commonly used:<br />

// single argument, single statement<br />

//arg => expression;<br />

var f1 = x => console.log("Just X");<br />

f1(); //Just X<br />

// multiple arguments, single statement<br />

//(arg1 [, arg2]) => expression;<br />

var f2 = (x,y) => x*y;<br />

console.log(f2(2,2)); //4<br />

// single argument, multiple statements<br />

// arg => {<br />

// statements;<br />

// }<br />

var f3 = x => {<br />

if(x>5){<br />

console.log(x);<br />

}<br />

else {<br />

console.log(x+5);<br />

}<br />

}<br />

f3(6); //6<br />

// multiple arguments, multiple statements<br />

// ([arg] [, arg]) => {<br />

// statements<br />

// }<br />

var f4 = (x,y) => {<br />

if(x!=0 && y!=0){<br />

return x*y;<br />

}<br />

}<br />

console.log(f4(2,2));//4<br />

[ 177 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!