Function in JavaScript
In Javascript,a function is a reusable block of code that can be defined and then executed whenever needed.
Syntax
function name(){ console.log("Hello"); }
What will be the output of above Code?
Nothing,it will not Print Anything. Why? because we have not called this function. Let’s see how to do this.
function fn(){ console.log("Hello"); } fn();
Function Keyword - The function keyword is used to create the function.
Function Name - The name of the function is fn, followed by parentheses ().
Function Body - The code that is executed when we call the function. In our case, it is console.log("Hello");
Function parameters
function sayhello(name){ console.log("Hello",name); } sayhello("kavya"); //Output: hello kavya
- Parameter (name) → The variable inside the function definition.
- Argument ("kavya") → The actual value passed when calling the function.
a function can have multiple parameters
function sayhello(name){ console.log(name); } sayhello(); //Output: undefined
if no value is passed for a parameter,it becomes undefined
Top comments (0)