0% found this document useful (0 votes)
35 views

Javascript E6

The document contains code snippets demonstrating the use of arrow functions in JavaScript. It defines a simple add function using the traditional function syntax and arrow function syntax. It then shows how arrow functions can be used to define a greet function with one parameter and return a message. Examples are given of arrow functions with no parameters and using arrow functions within the forEach method to execute a function for each array element.

Uploaded by

Java Jimmy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Javascript E6

The document contains code snippets demonstrating the use of arrow functions in JavaScript. It defines a simple add function using the traditional function syntax and arrow function syntax. It then shows how arrow functions can be used to define a greet function with one parameter and return a message. Examples are given of arrow functions with no parameters and using arrow functions within the forEach method to execute a function for each array element.

Uploaded by

Java Jimmy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

E6 Javascript

If you use a larger dish than what is recommended

SELECT ct.ID, ct.Name, ord.Name, ord.Amount


FROM customers AS ct, orders AS ord
WHERE ct.ID=ord.Customer_ID
ORDER BY ct.ID;

<form onsubmit="return validate()" method="post">


Number: <input type="text" name="num1" id="num1" />
<br />
Repeat: <input type="text" name="num2" id="num2" />
<br />
<input type="submit" value="Submit" />
</form>

function validate() {
var n1 = document.getElementById("num1");
var n2 = document.getElementById("num2");
if(n1.value != "" && n2.value != "") {
if(n1.value == n2.value) {
return true;
}
}
alert("The values should be equal and not blank");
return false;
}
function add(x, y) {
var sum = x+y;
console.log(sum);
}

function add(x, y) {
var sum = x+y;
console.log(sum);
}

const add = (x, y) => {


let sum = x + y;
console.log(sum);
}

This new syntax is quite handy when you just need a simple function with one
argument.
You can skip typing function and return, as well as some parentheses and braces.
For example:
const greet = x => "Welcome " + x;

The code above defines a function named greet that has one argument and returns a
message.

If there are no parameters, an empty pair of parentheses should be used, as in


const x = () => alert("Hi");

The syntax is very useful for inline functions. For example, let's say we have an array,
and for each element of the array we need to execute a function. We use
the forEach method of the array to call a function for each element:
var arr = [2, 3, 7, 8];
arr.forEach(function(el) {
console.log(el * 2);
});

However, in ES6, the code above can be rewritten as following:


const arr = [2, 3, 7, 8];
arr.forEach(v => {
console.log(v * 2);
});

You might also like