Javascript E6
Javascript E6
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);
}
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.
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);
});