Functions in JavaScript

작성자

카테고리:

← 피드로
DEV Community · Pranay Rebeyro · 2026-07-12 개발(SW)

Pranay Rebeyro

Function
Function is a reusable block of code designed to perform a specific task

Eg:
function greet() {
    console.log("Hello, World!");
}

greet(); // Output: Hello, World!

Enter fullscreen mode Exit fullscreen mode

Function with Parameters
Sometimes we want to pass values to a function. These values are called parameters.

Eg:
function greet(name) {
    console.log("Hello " + name);
}

greet("Pranay"); // Output: Hello Pranay

Enter fullscreen mode Exit fullscreen mode

Function with Multiple Parameters
A function can have more than one parameter.

Eg:
function add(a, b) {
    console.log(a + b);
}

add(10, 20); // Output: 30

Enter fullscreen mode Exit fullscreen mode

Function with Return Value
Sometimes we don’t want to print the result directly. Instead, we want the function to send the result back.For that, we use the return keyword.

Eg:
function add(a, b) {
    return a + b;
}

let result = add(10, 20);

console.log(result); // Output: 30

// The return statement ends the function and returns the value.

Enter fullscreen mode Exit fullscreen mode

Function Expression
A function can also be stored inside a variable.

Eg:
const greet = function() {
    console.log("Hello JavaScript");
};

greet(); // Output: Hello JavaScript

Enter fullscreen mode Exit fullscreen mode

Arrow Function
Arrow functions are a shorter way to write functions.

**Arrow Function**
Eg:
const greet = () => {
    console.log("Hello JavaScript");
};

greet(); //Output: Hello JavaScript

**Arrow Function with Parameters**
Eg:
const add = (a, b) => {
    return a + b;
};

console.log(add(10, 20)); // Output: 30

// If the function has only one line, we can write it even shorter.

Eg:
const add = (a, b) => a + b;

console.log(add(10, 20)); // Output: 30

Enter fullscreen mode Exit fullscreen mode

Anonymous Function
A function without a name is called an Anonymous Function.

Eg:
const message = function() {
    console.log("Welcome");
};

message(); // Output: Welcome

// Here, the function has no name and is stored inside the variable message.

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

추출 본문 · 출처: dev.to · https://dev.to/pranay_7424/functions-in-javascript-6ob

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다