Arrow function javascript luas lingkaran

Program menghitung luas lingkaran menggunakan javascript – pesonainformatika.com. Setelah sebelumnya kita telah membuat contoh menghitung luas segitiga sekarang kita lanjut membuat contoh lain yaitu luas lingkaran. Konsepnya hampir mirip, kita tinggal hanya mengubah rumusnya.

Haloooo apa kabar brader and sister, semoga sehat selalu ya. Yuk kita lanjut belajar javascript, kita kuatkan lagi logika kita dan pemahaman syntax dari bahasa ini dengan membuat contoh-contoh studi kasus. Okay kita masuk ke coding, let’s goooo. Eh kalau belum install node.js install dulu ya di //nodejs.org/en/download/

Bentuk dasar

Apa sih rumus dari luas lingkaran? tentunya kita harus tau dong. Luas lingkaran yaitu phi * r * r . Langsung cus perhatikan code dibawah ini:

const phi = 3.14
let r = 7
let L = phi * (r * r)

console.log('Luas: '+ L)

Penjelasan:

  1. kita membuat variable constant ( variable yang tidak dapat diubah nilainya) phi dengan nilai 3.14
  2. kita membuat variable r dengan value 7
  3. buat variable L untuk menyimpan nilai Luas. kita isi dengan rumus lingkaran
  4. console.log buat mencetak hasilnya

Setelah kita tahu bentuk dasar seperti diatas, kita bisa kreasikan dengan membuat fungsi khusus luas dengan parameter ‘r’

function luas(r) {
const phi = 3.14
L = phi * (r * r)
return L
}

let r = 7
let luas_lingkaran = luas(r)
console.log('Luas: '+luas_lingkaran)

penjelasan:

  1. kita buat fungsi luas dengan bersedia menerima parameter
  2. kita isi dalam fungsi yaitu variable phi , dan variable L
  3. fungsi mengembalikan nilainya dengan ‘return’ L
  4. buat variable r dengan nilai 7
  5. buat variable luas_lingkaran dengan nilai dari fungsi luas() dengan mengirim parameter r
  6. mencetak hasilnya dengan perintah console.log

Kira-kira bisa dipahami kan? bisa lah ya… hehe. Contoh-contoh studi kasus seperti semoga dapat membantu kita yang sedang mendalami pemrograman. Bagaimanapun untuk melatih logic dan penulisan, caranya memang ‘perbanyak praktik’. Intinya kita terus belajar dan belajar 😀

Untuk contoh studi kasus javascript lainya kita bisa pergi ke halaman berikut: Belajar javacript. Kita juga banyak post studi kasus di beberapa bahasa pemrograman seperti python, cpp, dan java. Semoga bermanfaat postingan tentang Program menghitung luas lingkaran menggunakan javascript kali ini, semoga bermanfaat, kita akan bertemu di next artikel.

An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:

  • Arrow functions don't have their own bindings to async param => expression async (param1, param2, ...paramN) => { statements } 6, async param => expression async (param1, param2, ...paramN) => { statements } 7, or async param => expression async (param1, param2, ...paramN) => { statements } 8, and should not be used as methods.
  • Arrow functions cannot be used as constructors. Calling them with async param => expression async (param1, param2, ...paramN) => { statements } 9 throws a // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 0. They also don't have access to the // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 1 keyword.
  • Arrow functions cannot use // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 2 within their body and cannot be created as generator functions.

param => expression (param) => expression (param1, paramN) => expression param => { statements } (param1, paramN) => { statements }

Rest parameters, default parameters, and destructuring within params are supported, and always require parentheses:

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression

Arrow functions can be // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 3 by prefixing the expression with the // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 3 keyword.

async param => expression async (param1, param2, ...paramN) => { statements }

Let's decompose a traditional anonymous function down to the simplest arrow function step-by-step. Each step along the way is a valid arrow function.

Note: Traditional function expressions and arrow functions have more differences than their syntax. We will introduce their behavior differences in more detail in the next few subsections.

// Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100;

In the example above, both the parentheses around the parameter and the braces around the function body may be omitted. However, they can only be omitted in certain cases.

The parentheses can only be omitted if the function has a single simple parameter. If it has multiple parameters, no parameters, or default, destructured, or rest parameters, the parentheses around the parameter list are required.

// Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100;

The braces can only be omitted if the function directly returns an expression. If the body has additional lines of processing, the braces are required — and so is the // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 5 keyword. Arrow functions cannot guess what or when you want to return.

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

Arrow functions are always unnamed. If the arrow function needs to call itself, use a named function expression instead. You can also assign the arrow function to a variable so it has a name.

// Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100;

Arrow functions can have either a concise body or the usual block body.

In a concise body, only a single expression is specified, which becomes the implicit return value. In a block body, you must use an explicit // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 5 statement.

const func = (x) => x * x; // concise body syntax, implied "return" const func2 = (x, y) => { return x + y; }; // with block body, explicit "return" needed

Returning object literals using the concise body syntax // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 7 does not work as expected.

const func = () => { foo: 1 }; // Calling func() returns undefined! const func2 = () => { foo: function () {} }; // SyntaxError: function statement requires a name const func3 = () => { foo() {} }; // SyntaxError: Unexpected token '{'

This is because JavaScript only sees the arrow function as having a concise body if the token following the arrow is not a left brace, so the code inside braces ({}) is parsed as a sequence of statements, where // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 8 is a label, not a key in an object literal.

To fix this, wrap the object literal in parentheses:

const func = () => ({ foo: 1 });

Arrow function expressions should only be used for non-method functions because they do not have their own async param => expression async (param1, param2, ...paramN) => { statements } 6. Let's see what happens when we try to use them as methods:

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 0

Another example involving // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 0:

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 1

Because a class's body has a async param => expression async (param1, param2, ...paramN) => { statements } 6 context, arrow functions as class fields close over the class's async param => expression async (param1, param2, ...paramN) => { statements } 6 context, and the async param => expression async (param1, param2, ...paramN) => { statements } 6 inside the arrow function's body will correctly point to the instance (or the class itself, for static fields). However, because it is a closure, not the function's own binding, the value of async param => expression async (param1, param2, ...paramN) => { statements } 6 will not change based on the execution context.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 2

Arrow function properties are often said to be "auto-bound methods", because the equivalent with normal methods is:

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 3

Note: Class fields are defined on the instance, not on the prototype, so every instance creation would create a new function reference and allocate a new closure, potentially leading to more memory usage than a normal unbound method.

For similar reasons, the // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 5, // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 6, and // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 7 methods are not useful when called on arrow functions, because arrow functions establish async param => expression async (param1, param2, ...paramN) => { statements } 6 based on the scope the arrow function is defined within, and the async param => expression async (param1, param2, ...paramN) => { statements } 6 value does not change based on how the function is invoked.

Arrow functions do not have their own async param => expression async (param1, param2, ...paramN) => { statements } 7 object. Thus, in this example, async param => expression async (param1, param2, ...paramN) => { statements } 7 is a reference to the arguments of the enclosing scope:

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 4

Note: You cannot declare a variable called async param => expression async (param1, param2, ...paramN) => { statements } 7 in , so the code above would be a syntax error. This makes the scoping effect of async param => expression async (param1, param2, ...paramN) => { statements } 7 much easier to comprehend.

In most cases, using rest parameters is a good alternative to using an async param => expression async (param1, param2, ...paramN) => { statements } 7 object.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 5

Arrow functions cannot be used as constructors and will throw an error when called with async param => expression async (param1, param2, ...paramN) => { statements } 9. They also do not have a // Traditional anonymous function (function (a, b) { const chuck = 42; return a + b + chuck; }); // Arrow function (a, b) => { const chuck = 42; return a + b + chuck; }; 6 property.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 6

The // Traditional anonymous function (function (a) { return a + 100; }); // 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }; // 2. Remove the body braces and word "return" — the return is implied. (a) => a + 100; // 3. Remove the parameter parentheses a => a + 100; 2 keyword cannot be used in an arrow function's body (except when used within generator functions further nested within the arrow function). As a consequence, arrow functions cannot be used as generators.

An arrow function cannot contain a line break between its parameters and its arrow.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 7

For the purpose of formatting, you may put the line break after the arrow or use parentheses/braces around the function body, as shown below. You can also put line breaks between parameters.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 8

Although the arrow in an arrow function is not an operator, arrow functions have special parsing rules that interact differently with operator precedence compared to regular functions.

(a, b, ...r) => expression (a = 400, b = 20, c) => expression ([a, b] = [10, 20]) => expression ({ a, b } = { a: 10, b: 20 }) => expression 9

Because // Traditional anonymous function (function (a, b) { const chuck = 42; return a + b + chuck; }); // Arrow function (a, b) => { const chuck = 42; return a + b + chuck; }; 8 has a lower precedence than most operators, parentheses are necessary to avoid // Traditional anonymous function (function (a, b) { const chuck = 42; return a + b + chuck; }); // Arrow function (a, b) => { const chuck = 42; return a + b + chuck; }; 9 being parsed as the arguments list of the arrow function.

async param => expression async (param1, param2, ...paramN) => { statements } 0

async param => expression async (param1, param2, ...paramN) => { statements } 1

The // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 5, // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 6, and // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 7 methods work as expected with traditional functions, because we establish the scope for each of the methods:

async param => expression async (param1, param2, ...paramN) => { statements } 2

With arrow functions, since our // Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100; 3 function is essentially created on the // Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100; 4 (global) scope, it will assume async param => expression async (param1, param2, ...paramN) => { statements } 6 is the // Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100; 4.

async param => expression async (param1, param2, ...paramN) => { statements } 3

Perhaps the greatest benefit of using arrow functions is with methods like // Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100; 7 and // Traditional Function function bob(a) { return a + 100; } // Arrow Function const bob2 = (a) => a + 100; 8 that usually require some kind of closure, // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 5, // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 6, or // Traditional anonymous function (function (a, b) { return a + b + 100; }); // Arrow function (a, b) => a + b + 100; const a = 4; const b = 2; // Traditional anonymous function (no parameters) (function() { return a + b + 100; }); // Arrow function (no arguments) () => a + b + 100; 7 to ensure that the function is executed in the proper scope.

Apa yang dimaksud arrow function dalam Javascript?

Arrow functions adalah tambahan baru ES6 yang memberi kita alternatif untuk regular functions. Arrow functions tidak cocok sebagai metode dan tidak dapat digunakan sebagai constructor.

Apa kelebihan menggunakan Arrow function?

dengan menggunakan arrow function pada javascript kita bisa lebih menghemat waktu pada saat mendevelop sebuah script javascript, selain itu arrow function ini juga akan sangat berguna saat kita membuat aplikasi web/android menggunakan framework javascript, seperti React dan React native nantinya.

Apa yang dimaksud dengan ES6 Javascript?

Apa itu ES6 Sekilas mengenai ES6, ES6 adalah kependekan dari EcmaScript 6, standarisasi kode Javascript bernama ECMAScript atau ES. dengan fitur let, const, Arrow Function, Spread properties dan spread operator, Class.

Postingan terbaru

LIHAT SEMUA