FUNCTION JAVASCRIPT
Disusun oleh :
Fika Baraka
11220930000090
PROGRAM STUDI SISTEM INFORMASI
FAKULTAS SAINS DAN TEKNOLOGI
UNIVERSITAS ISLAM NEGERI SYARIF HIDAYATULLAH
JAKARTA
2022
Introduction
Functions are one of the fundamental building blocks in JavaScript, used
to define reusable pieces of code. A function in JavaScript is a set of
statements that performs a task or calculates a value. Functions are also
referred to as methods, subroutines, or procedures in other
programming languages.
Defining a Function
To define a function in JavaScript, we use the function keyword, followed
by the name of the function and a set of parentheses, which can
optionally contain one or more parameters separated by commas. The
function body is enclosed in curly braces {}.
function functionName(parameter1, parameter2) {
// function body
Calling a Function
To call a function in JavaScript, we use the function name followed by a
set of parentheses that can optionally contain one or more arguments,
separated by commas.
functionName(argument1, argument2);
Returning a Value
Functions in JavaScript can also return a value. To do this, we use the
return keyword, followed by the value that we want to return.
function add(a, b) {
return a + b;
}
Function Parameters
Function parameters are variables that are defined in the function
definition, and they receive values when the function is called.
Parameters are listed in the parentheses of the function definition,
separated by commas.
function greet(name) {
console.log(`Hello, ${name}!`);
greet('Alice'); // Output: Hello, Alice!
Function Arguments
Function arguments are the values that are passed to the function when
it is called. Arguments are listed in the parentheses of the function call,
separated by commas.
function add(a, b) {
return a + b;
let result = add(2, 3); // result is now 5
Default Parameters
In ES6, default function parameters were introduced, which allow us to
specify default values for parameters in case they are not provided when
the function is called.
function greet(name = 'World') {
console.log(`Hello, ${name}!`);
greet(); // Output: Hello, World!
Arrow Functions
Arrow functions are a concise syntax for writing functions in JavaScript.
They were introduced in ES6 and are commonly used in modern
JavaScript code.
const add = (a, b) => a + b;
Conclusion
Functions are a fundamental concept in JavaScript and are used to write
reusable pieces of code. They can take parameters, return values, and
be defined using the function keyword or the arrow function syntax.
Understanding how to write and use functions is an essential skill for any
JavaScript developer.
DAFTAR PUSTAKA
"JavaScript: The Definitive Guide" oleh David Flanagan
"Eloquent JavaScript" oleh Marijn Haverbeke
"You Don't Know JS" oleh Kyle Simpson
"Professional JavaScript for Web Developers" oleh Nicholas C. Zakas
"JavaScript: The Good Parts" oleh Douglas Crockford