Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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; };
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

name arrow function

//Long version  
function add(a, b) {  
    return a + b;   
}

// Shorthand  
const add = (a, b) => a + b;   
Comment

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
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

javascript arrow function

const welcome = () => {
	console.log("THIS IS A ARROW FUNCTION")
}
Comment

Javascript basic arrow function

const power = (base, exponent) => {
    let result = 1;
    for (let count = 0; count < exponent; count++) {
      result *= base;
    }
    return result;
  };
Comment

arrow function

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

shorthand arrow function

const = newfunc = (a,b)=> 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

JavaScript Arrow Function

// function expression
let x = function(x, y) {
   return x * y;
}
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

javascript Arrow Function Syntax

let myFunction = (arg1, arg2, ...argN) => {
    statement(s)
}
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 function Java Script

param => expression
(param) => expression

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

Comment

Arrow functions syntax

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

PREVIOUS NEXT
Code Example
Javascript :: What is array.push in javascript 
Javascript :: javascript clone element 
Javascript :: string splice 
Javascript :: jquery get element by tag 
Javascript :: round number javascript 
Javascript :: scroll up link 
Javascript :: simple user agent parse js 
Javascript :: convert json to excel in javascript 
Javascript :: how custom angular material component date format 
Javascript :: angular9 spy 
Javascript :: destructured object 
Javascript :: javascript copy object 
Javascript :: jest test navlink 
Javascript :: find longest palindrome javascript algorithm 
Javascript :: headless ui modal 
Javascript :: jquery slider 
Javascript :: navigation prompt javascript 
Javascript :: angular input date pattern validation 
Javascript :: URLSearchParams 
Javascript :: google sheets get ranges 
Javascript :: convert array to object with custom keys 
Javascript :: cm to inches 
Javascript :: how to give path of file which in directory in require_once 
Javascript :: round down html 
Javascript :: what is closure in javascript 
Javascript :: how to assign onEdit to specigfic tab 
Javascript :: Javascript Unordered List HTML form Array 
Javascript :: react carousel 
Javascript :: transformar moeda real javascript 
Javascript :: javascript js ternary operater 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =