Conditions and Decisions


One is the most important thing while writing the code is condition, decisions are taken by the developers when they creating a software and than taken by user when they using this software. Because we are developers have ability to add intelligence by taking decisions in our code when certain area happen than do this another do else. So in this lesson we focus on heart of decisions or about some true/false conditions and some comparison operators.

Operators --

Comparison Operators :

image

Conditions : Using If...else statement


Syntax :

if(condition = true) {
alert("Condition is true.");
}else{
alert("Condition is false.");
}

Example:

if ( 10 > 5 ) {
alert ("10 is greater than 5.");
}else{
alert("Condition is not true.");
}
alert("This is outside of the if statement.");

If Statement Using Multiple Condition:

var foo = "Hello";
if(foo == "hello"){
alert ("hello");
}else if(foo == "HeLlo"){
alert("HeLlo");
}else if(foo == "HellO"){
alert("HellO");
}else{
alert ("No match found.");
}
alert("This is outside of if statement.")

Logical AND && operator : Logical operator use boolean values(true/false).

Do you like cake AND pie ?
true && true = true
true && false = false
false && true = false
false && false = false

Example :

var foo = "hello",
bar = 5;
if(foo == "hello" && bar < 6){
alert("true");
}{
alert("false");
}

Logical OR || operator : Logical operator use boolean values(true/false).

Do you like cake OR pie ?
true || true = true
true || false = true
false || true = true
false || false = false

Example :

var foo = "hello",
bar = 10;
if(foo == "hello" && bar < 6){
alert("true");
}{
alert("false");
}

Logical NOT ! operator : Logical operator use boolean values(true/false).

! true = false
! false = true

Example:

if(!(foo != "hello")){
alert("true");
}else{
alert("false");
}

Undefined and Null values : If we not implied or defined any value into var it's maintain automatically a undefined value and undefined value is true value but a null value is false value.


Example of Undefined :

var foo;
if(" "){ //if we not set a space into " " colons that its not take this value as undefined.
alert("Hi!");
}else{
alert("Haven't contain value")
}

Example of Null :

var foo ;
if(null){
alert("Hi!");
}else{
alert("Haven't contain value")
}



Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting