6. Scope & Conditional Statements : JAVASCRIPT

Scope & Conditional Statements

Scope & Conditional Statements

Scope - The worth of the variable gets effectively changed and it can make an issue on the off chance that we don't recollect the underlying worth given the variable. It is because the extent of var is global here. Yet, assuming we announce the var inside any capacity, its degree will stay under that capacity. 


In any case, it is energetically prescribed to utilize let rather than var to stay away from the disarray of the extent of the variable. 


Const - The worth can't be changed further using any means. Thus, it shows a blunder.


Conditional Statements -

let age = 5;

if(age>=19){

console.log("You can drink");

}

else if(age==2){

console.log("Age is 2")

}

else if(age==5){

        console.log("Age is 5")

}

else{

        console.log("You can drink Cold Drink");

}

This code shows the worth 5 in the control center. It is because we set the age at first as 5 and it prints the condition which is valid for that specific assertion. It is known as an if-else stepping stool or Ladder.


const cups = 41; 

switch (cups) {

case 4:

        console.log("The value of cups is 4")

        break;

case 41:

        console.log("The value of cups is 41")

        break;

case 42:

        console.log("The value of cups is 42")

        break;

case 43:

        console.log("The value of cups is 43")

        break;    

default:

        console.log("The value of cups is none of 4, 41, 42, 43")

        break;

}

This code shows the worth 41 in the control center. It is because the case has coordinated exactly with the worth of cups. In any case, if we eliminate the break connection, every one of the qualities will be printed. What's more, if, no worth matches with any case, then, at that point, the default explanation will be printed.

Post a Comment

0 Comments