Header Ads Widget

Responsive Advertisement

break statment class on 18/5/2022

 Break Statement:

The break statement is used to terminate the execution of a loop or switch statement.
When encountered the break statement causes the program flow to exit the current
loop or switch block and resume execution at the next statement after the loop or
switch.

for(let i = 1; i<=5; i++){
if(i === 3){
break;
}
console.log(i);
}

console.log("Hello everyone");

Continue statement:
The continue statement is used to skip the current iteration of a loop and move to
the next iteration. When encountered the continue statement causes the loop to skip
any remaining statements within the loop block and move to the next iteration.

for(let i = 1; i <= 10; i++){
if(i %3 === 0){
continue;
}
console.log(i);
};
Functions:
1) A function is a block of code that specifies the actions to be executed when the
function is invoked.
2) it consists of the function keyword followed by the function name, parenthesis
and the function body enclosed in curly braces.

syntax to declare a function:
function nameOfFunction(){
// function body
}

// declaring the function
function welcome(){
console.log("Hello welcome to the world");
}
// call the function
welcome();
Function Parameters:
1) Function parameters are variables that are listed as part of the function
definition.
2) They act as placeholders for values that will be passed to the function when
it is invoked.
3) parameters are specified inside the parenthesis of the function definiton.

function welcome(name){
console.log("Hello" + " " + name);
}
let name = prompt("Enter your name");
welcome(name);


// add two numbers using function
function add(a, b){
console.log( a + b);
}

add(2,4);
add(4,6);
add(6,8);
add(8,10);
add(10,12);
add(12,14);

Function return (Return statement):
The return statement is used to end the execution of a function and specify the
value to be returned to the caller. It is typically used to return a value or
terminate a function prematurely.

function add(a,b){
return a + b;
}

let result = add(63,78);
console.log(result);

function multiply(x, y){
return x * y;
}

let str1 = 'a';
let str2 = '45';

console.log(typeof(str1));
console.log(typeof(str2));

const result1 = multiply(str1,str2);
console.log(result1);
console.log(typeof(result1));

Function apply:
1) The apply() method is a built-in function in js that allows you to call a
function with a given this value and arguements provided as an array or array-like
objects.
2) It is primarily used in scenarios where you want to invoke a function with a
dynamic number of arguements.

function person(a, b){
return a + b;
}

let num = [5,5];
console.log(typeof(num));
const result = person.apply(null,[5,5]);
console.log(result);
console.log(typeof(result));

Function Bind:
1) The bind() method is built-in function
in js that creates a new function with a specific this value and optionally,
pre-filled arguements.
2) It allows you to create a new function that, when invoked, has its value set to
a specified object.

function person(name){
console.log("Hello"+ " " + name);
}
let person1 = person.bind(null, "pranesh");
person1();

Post a Comment

0 Comments