Regular function vs Arrow Function Javascript

Regular function dapat berupa function declaration atau function expression, namun arrow function hanya berupa function expression saja. Itu sebabnya arrow function memiliki nama lengkap “arrow function expressions”.

  1. // function declaration

  2. function sayHello(greet) {

  3.     console.log(`${greet}!`);

  4. }

  5.  

  6.  

  7. // function expression

  8. const sayName = function (name) {

  9.     console.log(`Nama saya ${name}`)

  10. }



  1. // function expression

  2. const sayHello = greet => console.log(`${greet}!`);

  3. const sayName = name => console.log(`Nama saya ${name}`);



Karena arrow function merupakan sebuah expression, maka ia hanya dapat digunakan untuk disimpan pada variabel (seperti contoh kode di atas), sebagai argumen pada sebuah fungsi, dan sebagai nilai dari properti objek.



  1. const sayName = name => console.log(`Nama saya ${name}`);




  1. ["Dimas", "Widdy", "Buchori"].forEach(name => console.log(`Nama saya ${name}`));




  1. const user = {

  2.     introduce: name => console.log(`Nama saya ${name}`)

  3. }



About the Author

Saya seorang lulusan SMK

Posting Komentar


Cookie Consent
Ikramlink.com serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.