← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 4 of 10

JS Functions

Think of a coffee machine, you put in water and coffee grounds, press a button, and out comes a cup of coffee, every single time, without you needing to know exactly how the machine works internally. A function in JavaScript is that same idea, a reusable block of code that takes some input, does something with it, and optionally hands back a result, letting you avoid rewriting the same logic over and over. Functions are everywhere in real JavaScript, from calculating a total to greeting a visitor by name on a page like cookiescursor.com.
Syntax
javascript
function functionName(parameter1, parameter2) {
  // body
  return value;
}

functionName(argument1, argument2);

Function Declaration

Function declaration function keyword से code का एक नामित, पुन: उपयोग योग्य block परिभाषित करता है, जिसे फिर उस नाम से कहीं भी बुलाया जा सकता है जहाँ वह scope में हो।

Note: Functions को क्रिया बताने वाले verbs से नाम दीजिए, जैसे calculateTotal या greetUser, इससे बुलाने वाला code खुद-समझाने वाला बनता है।
Warning: Two function declarations with the same name in the same scope silently overwrite each other, only the last one remains callable.

उदाहरण: Function Declaration

javascript
// Define the function `greetUser` with no parameters
// Define the function `greetUser` with no parameters
function greetUser() {
  // Print "Hello!" to the console
  // Print "Hello!" to the console
  console.log("Hello!");
}
// Call `greetUser()`
// Call `greetUser()`
greetUser();

Parameters और Arguments

Parameters function की परिभाषा में सूचीबद्ध नामित placeholders हैं, जबकि arguments वे असली values हैं जो function बुलाते समय दिए जाते हैं, parameters बताते हैं कि function क्या उम्मीद करता है, arguments उसे उपलब्ध कराते हैं।

Note: एक function comma से अलग किए कई parameters स्वीकार कर सकता है, और आप उन्हें कोई भी ऐसा नाम दे सकते हैं जो उनका उद्देश्य साफ़ बताए।
Warning: Calling a function with fewer arguments than parameters simply leaves the missing ones as undefined, JavaScript does not throw an error by default.

उदाहरण: Parameters and Arguments

javascript
function add(a, b) {
  console.log(a + b);
}
add(2, 3); // 2 and 3 are arguments

Return Values

return keyword किसी value को function से बाहर उसे बुलाने वाले तक भेजता है, और जैसे ही return चलता है, function तुरंत execute होना बंद कर देता है और उसके बाद के code को छोड़ देता है।

Note: function के returned value को variable में store कीजिए ताकि आप उसे बाद में इस्तेमाल कर सकें, केवल function के अंदर सीधे log करने की बजाय।
Warning: Without an explicit return statement, a function always produces undefined, even if it internally calculates something useful.

उदाहरण: Return Values

javascript
// Define the function `square` taking `n`
// Define the function `square` taking `n`
function square(n) {
  // Return `n * n` from this function
  // Return `n * n` from this function
  return n * n;
}
// Print `square(4)` to the console
// Print `square(4)` to the console
console.log(square(4));

Default Parameters

Default parameters आपको function की परिभाषा में ही एक बैकअप value देने देते हैं, जो अपने-आप तब इस्तेमाल होता है जब बुलाने वाला उस parameter के लिए कोई argument न दे।

Note: Default parameters, function body के भीतर हाथ से if (value === undefined) जाँचने का ज़्यादा साफ़ विकल्प हैं।
Warning: A default parameter only applies when the argument is undefined or omitted, passing null explicitly does not trigger the default.

उदाहरण: Default Parameters

javascript
// Define the function `greet` taking `name`
// Define the function `greet` taking `name`
function greet(name = "Guest") {
  // Print `Hello, ${name}!` to the console
  // Print `Hello, ${name}!` to the console
  console.log(`Hello, ${name}!`);
}
// Call `greet()`
// Call `greet()`
greet();
// Call `greet("Amit")`
// Call `greet("Amit")`
greet("Amit");

Function Expressions और Hoisting

Function expression किसी function को स्वतंत्र नाम देने की बजाय variable के अंदर रखता है, और function declarations के विपरीत, जो hoist होते हैं और अपने scope में कहीं भी बुलाए जा सकते हैं, function expressions केवल उस line के बाद उपयोग हो सकते हैं जहाँ वे परिभाषित हैं।

Note: Function expressions खास तौर पर तब उपयोगी हैं जब आपको किसी function को value के रूप में पास करना हो, जैसे किसी दूसरे function या event handler में।
Warning: Calling a function expression before its definition line throws an error, since only the variable itself, not its function value, is hoisted.

उदाहरण: Function Expressions and Hoisting

javascript
// Declare the constant `sayHi`, set to `function () {`
// Declare the constant `sayHi`, set to `function () {`
const sayHi = function () {
  // Print "Hi!" to the console
  // Print "Hi!" to the console
  console.log("Hi!");
};
// Call `sayHi()`
// Call `sayHi()`
sayHi();
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.