# JavaScript Basic notes 3.

# Functions In JavaScript...

## Q.1) Functions in Javascript

Ans. If we have more line codes and it was reusable many times then we can store on it a function and when we want to use these codes then we call the function name simply and our code were run.

If we have a big amount of apples and in future, we keep them in another place then we took these apples into a box and keep it in another place.

### Syntax:

`function function_name(){`

`// statement`

`}`

Let's see an example 👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686546502295/f9537592-9d0b-407d-9279-bba3cc325a9f.png align="center")

Remember one thing when you create a function after creating it is necessary to call it. like the upper image, I call a function in 4th line `[myfun()]`

## Q.2) Function with Parameters.

Ans. If the function work is the same but we want to minor changes in the function then we need to use parameters in a function.

### Syntax:

`function function_name(parameter1,parameter2,.......){`

`// statement...`

`}`

`function_name(values);`

Let's see with an example👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686547301348/5b126c7a-3e8b-4cfa-92c6-23fca11cb18a.png align="center")

## Q.3) Function with the return value.

Ans. If we want to not perform a code(like adding, sub, multiplication) into the function we can use a return statement in the function. If we want to reuse the code then we use the return statement.

### Syntax:

`function function_name(perameter1,perameter2,.....){`

`//statement`

`return value`

`}`

`function_name(values);`

Let's see with an example👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686548875120/08ea7876-afa2-4a6c-8c06-0080cdd0ea0e.png align="center")

## Q.4) Global and local variables.

Ans. Global variables are declared outside the function and the local variables are declared inside the function.

### Difference between Global and Local variables

1. `Global variables:`\- Global variables work inside the function and outside the function.
    
2. `Local Scope:`\- Local variables don't work on outside the function. It only works inside the function.
    

## Q.5) Events In JavaScript.

Ans. Basic events list:-

* Click
    
* Double Click
    
* Right Click
    
* Mouse Hover
    
* Mouse Out
    
* Mouse Down
    
* Mouse Up
    
* Key Press
    
* Key Up
    
* Load
    
* Unload
    
* Resize
    
* Scroll
    

## Q.6) Loops in JavaScript.

Ans. The basic definition of a loop is to print the statement continuously and give it several times. There are five types of loops In JavaScript which are following as:

1. while Loop
    
2. Do/while loop
    
3. for loop
    
4. for in loop --&gt; This is used with OBJECT.
    
5. forEach loop --&gt; This is used with Array
    

### While loop Syntex:

`var a = 1 --> initialization`

`while(a <=10){ ---> Condition`

`console.log("hurry");`

`a=a++ ---> increment/decrement`

`}`

Let's see with an example👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686567518013/64f50ec4-e345-4029-8164-76d70b50087d.png align="center")

### Do while loop Syntax;

`let a = 1; ---> Initialization`

`do{`

`console.log(a);`

`a = a++; ----> Increment/decrement`

`}while(a<=5) ----> condition`

Let's see with an example 👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686567871668/a685d9ed-11d5-477d-ac6f-92fedc3a87ef.png align="center")

### For loop Syntax in JS

`for(let a = 0; a<=10;a++){`

`console.log(a)`

`}`

Let's see with an example 👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686568153877/e388fe69-1235-435b-a847-97063436077c.png align="center")

If you want to read more about loop then click on this [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration).

## Q.7) Break and Continue In JS

Ans. This statement runs in conditional statements like we are running a loop statement and if we want to break a specific condition we use a break statement.

### Syntax:

`for (initialization;condition;increment/decrement){`

`//condition with if else`

`//statement`

`break/continue`

`}`

`console.log("message")`

Let's see with an example👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686568796918/8708a259-eccf-4ae5-8354-a53362a57368.png align="center")

This is for continuing statement👆

This is for the Break statement👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686568890760/8128e69f-16ba-41a7-aa59-bfb41c2fd0ba.png align="center")
