JavaScript Function

 

JavaScript Function

Function are one of the fundamental building blocks in JavaScript and function is basically a set of statements that performs the task or calculate the value.

For example:

function greet(){}

function Function keyowrd

greet is the Name

() To define function is a part of syntax

{} Body of the function this the where we add all the statement to define some kind of logic in our application.

function greet(){

console.log(‘Hello World’);



 }

When declaring functions, no need to add semicolon (;) at the end of curly bracket because we are not declaring It’s like a variable.


We can call this function like this

greet();

Save the changes Now check in console.

Answer: Hello world

Our functions can have input and this input can change how the function behave.


Name is an input to the function


Name is only Meaningful and useful inside the function it cannot be assessable outside the function


Only meaningful variable in between prentices refer to this variable has a parameter

function greet(name){


console.log(‘Hello’ + name);

}



greet (‘John’)

+ is to concatenate two strings.

To call the greet function, we need to pass the value for the name variable or name parameter more accurately.


We refer to this is an argument, so John is an argument to the greet function, and the name is the parameter of the greet function.

So, we need to know what is parameter and what is argument

Argument is the actual values supply for the parameter.

greet (‘John’);

greet (‘Mary’);

 So, function can have multiple parameters and multiple arguments.

For example:


Parameters separated by comma ,         


function greet(name,last name){

console.log(‘Hello’ + name +’ ’+ last name)

}

greet (‘John’, ‘Smith’);

Now check in console

Answer: Hello John Smith,

The default value of variable in JavaScript is undefined.

Type of function



Performing a task, This task is to display. Something on console.

function greet(name, lastName){

console.log(‘Hello’ + name + ‘ ‘+lastName)

}
greet (‘John’,’Smith’);

But sometimes our function is to calculate something.


Function takes the parameter called number.

Calculating a value.


Name of the function



functions square(number) {



return number*number;

}

To return the value whoever calling this function. To calculate square of the number

 





let number = square(2);


Console.log(number);

so we can declare another variable call number and set it to square(2).

This return value. We can use that value to initialized a variable

Now it will display on console



or simply pass console.log(Square(2));

When the JavaScript Engine execute, this code first is going to call this function It will get the value and pass that value to console.log and save the change. it will still get the same answer in console

Answer: 4

So how many function calls do we have in this code we have two function called square of 2 is a function call and also console.log(); Is also a another function call.

Post a Comment

Share your thoughts here

Previous Post Next Post