Ads Top

What is javascript conditional statement?

Javascript conditions:

if: If condition will be true then block of code will be execute otherwise run code will be jump on the other block of code. IF (if) the reserved keyword of the Javascript and also we use only small letter as in brackets (if). We can't use as "IF","If","iF". And if statement always run condition with result of "true".

else: If condition will be false then block of code will be execute otherwise run code will be jump on the other block of code if we are using another "if". "else" is the reserved keyword of the Javascript and also we use only small letter as in brackets (else) .And "else" statement always run condition with result of "false".

Syntax

<script>
if (condition) {
// block of code .... if condition will be true.....
}

// if else....................
if (condition) {
// block of code .... if condition will be true.....
} else {
// block of code .... if condition will be false.....
}

// nested if else.................
if (condition) {
// block of code .... if condition will be true.....
} else if (condition) {
// block of code .... if condition will be false.....
} else {
// block of code .... if condition will be false.....
}
</script>

If else example below:

<script>
var a = 5;
if (a==5)
document.write(a);
// another ..............
var a = 5;
var b = 10;
if ((a==5) && (b==10))
document.write("And condition is now corrected");
</script>

Switch case:

Switch: If else and Switch case both behavior is same as condition almost same. if condition's execution is slow(in case it's on the large number as 100+) because it's going and check condition one by one if in case it's in small number then it's go to fast execution and it's not take any memory allowcation.

And Switch: need to use in when we require in the large number size because when it's go to the execution process he take memory allowcation first then it's ready for the action so it's not take a lot of time and give result immediately. Switch work depend on the expression and expression will be a Strings or numbers not condition

Syntax of Switch

<script>
switch(expression) {
case x:
// code according to requirement
break;
case y:
// code according to requirement
break;
default:
// default code according to requirement
}

// another way default in the beginning..............

switch(expression) {
default:
// default code according to requirement
case x:
// code according to requirement
break;
case y:
// code according to requirement
break;
}
</script>

Also you can write default Section on the top of beginning code"default"

Switch example below:
<script>
var day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
default:
day = "Holiday :)";
}
</script>


No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.