DekGenius.com
JAVASCRIPT
JavaScript arrow functions default parameter
const greeting = (name = "Anonymous") => "Hello " + name;
console.log(greeting("John"));
console.log(greeting());
arrow function javascript
// Normal Function in JavaScript
function Welcome(){
console.log("Normal function");
}
// Arrow Function
const Welcome = () => {
console.log("Normal function");
}
arrow function javascript
///////
//JavaScript Function Declarations
///////
//4th (arrow function)
hello4 = (name) => { return ("Hello " + name); }
//OR
hello5 = (name) => { return (`Hello new ${name}`) }
//1st (simple function)
function hello1() {
return "Hello simple function";
}
//2nd (functino expression)
hello2 = function() {
return "Hello functino expression";
}
// 3rd ( IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE))
hello3 = (function() {
return "Hello IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE)";
}())
arrow function javascript
// Function in JavaScript
function regular(){
console.log("regular function");
}
regular(); //regular function
// Arrow Function
const arrow = () => console.log("Arrow function");
arrow(); //Arrow function
javascript arrow function
// Non Arrow (standard way)
let add = function(x,y) {
return x + y;
}
console.log(add(10,20)); // 30
// Arrow style
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;
// You can still encapsulate
let add = (x, y) => { return x + y; };
Arrow functions in ES6
let func = (a) => {} // parentheses optional with one parameter
let func = (a, b, c) => {} // parentheses required with multiple parameters
Arrow functions in Javascript
/**
I think that you might be looking for
the js "arrow function"; I hope that
this example below helps ;)
**/
// usual function
function fartOne(){
console.log('Pooofff... pof.. ppf.. poof.. p');
}
// arrow function to do the same
const fartTwo = () => console.log('Baaaf... paf.. poof.. poffie.. plop');
// call the functions to test 'em out..
fartOne();
fartTwo();
how to write an arrow function in javascript?
function double(x) { return x * 2; } // Traditional way
console.log(double(2)) // 4
const double = x => x * 2; // Same function written as an arrow function with implicit return
console.log(double(2)) // 4
js arrow function
// const add = function(x,y) {
// return x + y;
// }
// const add = (x, y) => {
// return x + y;
// }
const add = (a, b) => a + b;
const square = num => {
return num * num;
}
// const rollDie = () => {
// return Math.floor(Math.random() * 6) + 1
// }
const rollDie = () => (
Math.floor(Math.random() * 6) + 1
)
javascript Arrow Function with One Argumen
let greet = x => console.log(x);
greet('Hello'); // Hello
arrow function javascript
([a, b] = [10, 20]) => a + b; // result is 30
({ a, b } = { a: 10, b: 20 }) => a + b; // result is 30
arrow function javascript
const suma = (num1, num2) => num1+num2
console.log(suma(2,3));
//5
javascript arrow function
const welcome = () => {
console.log("THIS IS A ARROW FUNCTION")
}
arrow function javascript
// Traditional Function
function (a, b){
return a + b + 100;
}
// Arrow Function
(a, b) => a + b + 100;
// Traditional Function (no arguments)
let a = 4;
let b = 2;
function (){
return a + b + 100;
}
// Arrow Function (no arguments)
let a = 4;
let b = 2;
() => a + b + 100;
arrow function javascript
// Traditional Function
function (a, b){
let chuck = 42;
return a + b + chuck;
}
// Arrow Function
(a, b) => {
let chuck = 42;
return a + b + chuck;
}
arrow function javascript
hello = () => {
return "Hello World!";
}
arrow func in javascript
const greet = (who) => {
return `Hello, ${who}!`;
};
greet('Eric Cartman'); // => 'Hello, Eric Cartman!'
es6 arrow function
//ES5
var phraseSplitterEs5 = function phraseSplitter(phrase) {
return phrase.split(' ');
};
//ES6
const phraseSplitterEs6 = phrase => phrase.split(" ");
console.log(phraseSplitterEs6("ES6 Awesomeness")); // ["ES6", "Awesomeness"]
es6 arrow function
const multiplyES6 = (x, y) => x * y;
Javascript basic arrow function
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
arrow function javascript
// an arrow function is also called a lambda or an anonymous function
let myFunction = () => {
// some logic
}
arrow functions javascript
// Traditional Function Expression
var add = function(a,b){
return a + b;
}
// Arrow Function Expression
var arrowAdd = (a,b) => a + b;
arrow function javascript
//arrow function
()=>{}
//normal function
function(){}
//useses of arrow function
var fnct=()=>{}
var fnct=(param1,param2,...rest)=>{console.log(param1),alert(param2),return(rest)}
//or these
var fnct=e=>{}
var fnct=(e)=>e
var fnct=e=>e
//examples
var fnct=param=>{return 'hello '+param}
var fnct=(param1,param2,...rest)=>!param1?param2:rest
var fnct=return_=>return_
var fnct=hi=>alert(hi)
Arrow function in ES6
var array = [1, 2, 3, 4]
const sum = (acc, value) => acc + value
const product = (acc, value) => acc * value
var sumOfArrayElements = array.reduce(sum, 0)
var productOfArrayElements = array.reduce(product, 1)
js arrow function
hello = () => {
return "Hi All";
}
arrow function javascript
hello4 = (name) => { return ("Hello " + name); }
//OR
hello5 = (name) => { return (`Hello new ${name}`) }
document.getElementById("arrow").innerHTML = hello4("arrow function");
document.write("<br>" + hello5("arrow function"));
arrow function javascript rules
const add3 = (num1, num2, num3) => return num1 + num2 + num3;
javascript Arrow Function with No Argument
let greet = () => console.log('Hello');
greet(); // Hello
arrow function javascript
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
//if the function got only one parameter
const square1 = (x) => { return x * x; };
const square2 = x => x * x;
// empty parameter
const horn = () => {
console.log("Toot");
};
arrow function javascript
// Traditional Function
function myFunction(param) {
var a = param * 3;
return a;
}
//Arrow Function
let myFunction = (a, b) => {
let c = (a * b) + 3;
return c;
}
javascript this Inside Arrow Function
const greet = () => {
console.log(this);
}
greet(); // Window {...}
JavaScript Arrow Function
// function expression
let x = function(x, y) {
return x * y;
}
arrow function javascript
params => ({foo: "a"}) // returning the object {foo: "a"}
javascript this inside arrow function
function A() {
this.val = "Error";
(function() {
this.val = "Success";
})();
}
function B() {
this.val = "Error";
(() => {
this.val = "Success";
})();
}
var a = new A();
var b = new B();
a.val // "Error"
b.val // "Success"
arrow function javascript rules
const square = num => num ** 2;
arrow function javascript rules
const sayHi = ()=>console.log('Hi');
es6 arrow function
// ES6
const prices = smartPhones.map(smartPhone => smartPhone.price);
console.log(prices); // [649, 576, 489]
Arrow Function Shorthand javascript
// Arrow Functions Shorthand javascript
// Longhand:
function sayHello(name) {
console.log('Hello', name);
}
sayHello("Chetan"); // Hello Chetan
setTimeout(function() {
console.log('Loaded'); //Loaded
}, 2000);
[1,2,3].forEach(function(item) {
console.log(item);
// 1
// 2
// 3
});
// Shorthand:
sayHello = name => console.log('Hello', name);
sayHello("Chetan"); //Hello Chetan
setTimeout(() => console.log('Loaded'), 2000); //Loaded
[1,2,3].forEach(item => console.log(item));
// 1
// 2
// 3
javascript Arrow Function Syntax
let myFunction = (arg1, arg2, ...argN) => {
statement(s)
}
Arrow functions javascript
// Arrow function with two arguments
const sum = (firstParam, secondParam) => {
return firstParam + secondParam;
};
console.log(sum(2,5)); // Prints: 7
// Arrow function with no arguments
const printHello = () => {
console.log('hello');
};
printHello(); // Prints: hello
// Arrow functions with a single argument
const checkWeight = weight => {
console.log(`Baggage weight : ${weight} kilograms.`);
};
checkWeight(25); // Prints: Baggage weight : 25 kilograms.
// Concise arrow functions
const multiply = (a, b) => a * b;
console.log(multiply(2, 30)); // Prints: 60
arrow function javascript
const square = num => num * num;
ES6 arrow functions in JavaScript
const greeting = () => console.log('Hello World');
arrow function javascript
// An empty arrow function returns undefined
let empty = () => {};
(() => 'foobar')();
// Returns "foobar"
// (this is an Immediately Invoked Function Expression)
var simple = a => a > 15 ? 15 : a;
simple(16); // 15
simple(10); // 10
let max = (a, b) => a > b ? a : b;
// Easy array filtering, mapping, ...
var arr = [5, 6, 13, 0, 1, 18, 23];
var sum = arr.reduce((a, b) => a + b);
// 66
var even = arr.filter(v => v % 2 == 0);
// [6, 0, 18]
var double = arr.map(v => v * 2);
// [10, 12, 26, 0, 2, 36, 46]
// More concise promise chains
promise.then(a => {
// ...
}).then(b => {
// ...
});
// Parameterless arrow functions that are visually easier to parse
setTimeout( () => {
console.log('I happen sooner');
setTimeout( () => {
// deeper code
console.log('I happen later');
}, 1);
}, 1);
arrow functions javascript
const myFunc = () => "value";
arrow function Java Script
param => expression
(param) => expression
// Arrow Function
(a, b) => {
const chuck = 42;
return a + b + chuck;
};
© 2022 Copyright:
DekGenius.com