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 👇

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👇

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👇

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 --> This is used with OBJECT.

  5. forEach loop --> 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👇

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 👇

For loop Syntax in JS

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

console.log(a)

}

Let's see with an example 👇

If you want to read more about loop then click on this link.

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👇

This is for continuing statement👆

This is for the Break statement👇

Did you find this article valuable?

Support Notes by becoming a sponsor. Any amount is appreciated!