DEV Community

Kavya S
Kavya S

Posted on

Day 3 - JavaScript

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");
}
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

if no value is passed for a parameter,it becomes undefined

Top comments (0)