Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Arrow functions in ES6

let func = (a) => {}         // parentheses optional with one parameter
let func = (a, b, c) => {} // parentheses required with multiple parameters
Comment

Arrow Functions

// The usual way of writing function
const magic = function() {
  return new Date();
};

// Arrow function syntax is used to rewrite the function
const magic = () => {
  return new Date();
};
//or
const magic = () => new Date();

Comment

arrow functions=>

// Traditional Anonymous Function
function (a, b){
  return a + b + 100;
}

// Arrow Function
(a, b) => a + b + 100;

// Traditional Anonymous 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;
Comment

arrow function

const doSomething = () => {
  // Function Scope
};
Comment

An Arrow Function

/*The arrow functions were introduced in ECMA 2015 with the main purpose of giving a shorter syntax to a function expression. 
Besides providing shorter syntax, which increases the readability of the code, 
it does not have its own value of the this object. The value of this object inside an arrow function is inherited from the enclosing scope.

You can write an arrow function to add two numbers as shown in the next code example.*/

var add = (num1, num2)=> num1+num2; 
let res = add(5,2);
console.log(res); // 7 
Comment

arrow function

const = newfunc =(a,b)=>{
	return a+b
}
Comment

Arrow Function Example

let nums = [3, 5, 7]
let squares = nums.map(function (n) {
  return n * n
})
console.log(squares)
Comment

arrow function

const hello = () => {  
 return 'Hello'
}

const hello = () => 'Hello'
console.log(hello())
Comment

arrow functions

let add = (x, y) => x + y;

console.log(add(10, 20)); // 30;
Code language: JavaScript (javascript)
Comment

arrow functions=>

(param1, paramN) => {
   let a = 1;
   return a + param1 + paramN;
}
Comment

arrow function

const bk = () =>{
   console.log("Bk function called");
}
bk()
Comment

arrow functions=>

// Traditional Anonymous Function
function (a, b){
  let chuck = 42;
  return a + b + chuck;
}

// Arrow Function
(a, b) => {
  let chuck = 42;
  return a + b + chuck;
}
Comment

arrow function

textBox.addEventListener('keydown', (event) => {
  console.log(`You pressed "${event.key}".`);
});
Comment

Arrow Function Example

let nums = [3, 5, 7]
let squares = nums.map((n) => {
  return n * n
})
console.log(squares)
Comment

arrow function

// Traditional Function
function (a){
  return a + 100;
}

// Arrow Function Break Down

// 1. Remove the word "function" and place arrow between the argument and opening body bracket
(a) => {
  return a + 100;
}

// 2. Remove the body brackets and word "return" -- the return is implied.
(a) => a + 100;

// 3. Remove the argument parentheses
a => a + 100;
Comment

arrow function

// 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 functionsconst multiply = (a, b) => a * b; console.log(multiply(2, 30)); // Prints: 60 
Comment

Arrow functions syntax

const introduction = () => {
    console.log("Hello, my name is Jessica.");
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: push to object javascript 
Javascript :: Angular empty object 
Javascript :: javascript map array 
Javascript :: javascript create json object from array 
Javascript :: js scroll to bottom exact 
Javascript :: javascript sleep 3 second 
Javascript :: javascript get cursor position without event 
Javascript :: delete file with deno 
Javascript :: react router dom v6 private route 
Javascript :: setimmediate 
Javascript :: react owl-carousel 
Javascript :: bootstrap 5 with next js 
Javascript :: javascript valueOf() Method 
Javascript :: node package.json type module 
Javascript :: text background fabricjs 
Javascript :: js array split by comma 
Javascript :: check if all elements in array match a condition javascript 
Javascript :: local storage for chrome extension 
Javascript :: how to insert div around element in javascript 
Javascript :: javascript how to merge arrays 
Javascript :: convert int to string in angular 
Javascript :: react js error boundary functional component 
Javascript :: days between two dates 
Javascript :: cypress wait object to change 
Javascript :: shorthand if statement js 
Javascript :: writefile in node js 
Javascript :: select multiple id in jquery 
Javascript :: react icon 
Javascript :: dynamic regex javascript 
Javascript :: nginx get request method 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =