FUNCTION AND CONDITIONS IN JAVASCRIPT
FUNCTIONS
A Function is a set of instructions or block of codes executed when it is called.
There are two types of functions:
- Built in Functions
- User Defined Functions
Build in Functions:
There are lot of functions already built in within the javascript.
javascript comes along with all these built-in functions.
Examples:
console.log()
document.queryselector()
parseInt()
parsefloat()
math.sign()
math.max()
User defined functions:
When the user creates their own functions then it is called as user defined functions.
How to define a function?
step1: type function
step2: type the function name
step3: open the curly brace , type the block of codes and close the curly braces
step4: call the function using the function name
syntax:
function function name{
function body
};
Example:
//Defining the function//
function add(){
const x=12;
const y =13;
const z=x+y;
return z
}
add; //calling the function//
Note: The return is a keyword in javascript . it is used to stop the execution of function and used to store the result.
Parameters and Arguments in the function:
parameters are nothing but the variables present inside the () brackets . It expects the function to pass some data to it.
Example:
function add(x,y){
const z= x+y;
return z
}
add(78,89)
when we pass some data or values to the function those values are called as arguments
Note: The parameters are nothing but the placeholders and the arguments are the actual values that you are passing to that placeholders.
conditions
conditional statements are used to simplify the work of a user . It is defined as that if the
condition is true then the set of instructions are executed otherwise not.
There are 4 types
- if condition
- else condition
- else if condition
- switch
If condition : If the condition is true then execute the following code
syntax: if (condition){
// code to be executed//
}
Else condition : If the same condition is false then execute the else condition
syntax: if (condition){
// code to be executed//
}
else{
// code to be executed//
}
Else if condition : If the first condition is false then execute this else if condition
syntax: if (condition){
// code to be executed//
}
else if(condition){
// code to be executed//
}
else{
// code to be executed//
}
Example:
if (day=="monday"){
console.log("sad😔")
}
else if(day=="sunday"){
console.log("happy😄")
console.log("ok😐")
}
Comments
Post a Comment