Home > Java Scripts Examples and Tutorials > JavaScript - Else If
India still ‘world class’ insists Mathews
SL to market Ayurveda drugs
Nine hand grenades found near Aluth Kade Court
Hirunika to join active politics
IPL teams splash the cash for Mahela and Murali
Anoma to lead protest to free SF
US diplomats heading to Sri Lanka
CID probing double murder in Kahawatta
Dr. R.H.S. Samaratunga appointed Sec to Petroleum Ministry
A/L fiasco - Probe team’s mandate limited
Three including police officer injured in Maligawatta shootout
External degrees are worthless says Minister S.B. Dissanayke
SL rules out currency devaluation in 2012
මාළිගාවත්තේ වෙඩි තැබීමක් -තිදෙනෙකුට තුවාලයි
ක්රිකට් මුල් පුටුව උපාලි ධර්මදාසට
Accidents on the rise as New Year dawns
CAA yet to act on gas price revision
38 students yet to receive A/L results - CTU
நாடாளுமன்ற தெரிவுக்குழுவை நாம் முற்றாக நிராகரிக்கவில்லை - சம்பந்தன்
மட்டு புதுவருட கொண்டாட்டங்கள்
Are you in Love?
10 Top Sex Secrets Men won't Tell
Top 10 — Female Turn Ons
10 Commandments for good wives
How to live a happy and satisfied Life
Did I marry the right person?
Are You Happy
Alcohol makes men BETTER in the bedroom, scientists claim
Too busy for Love Making?
Have you gone off sex?
Love Isn't Blind
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Are You Getting Attracted To Another Person?
Do you feel loved?
JavaScript - Else If
When the condition is 'true' the if statement block is executed while the statement block in else is executed when the condition is 'false'. However, as in life, things are rarely black or white... there are always shades of grey.
The JavaScript if - else statements also provides for else if clauses through which we can make additional branches to the condition.
if (number > 0)
{
alert("Number is a positive integer");
}
else if (number < 0)
{
alert("Number is a negative integer");
}
else
{
alert("Number is 0");
}
Note that the else if clause is followed by another condition placed between parenthesis. If this condition is true, the statement inside the else if block are executed.
The code above checks the value of variable number. When number is greater than 0, the statements in if block are executed and when number is less than 0, the statements in else if block take over. Finally, if the number is equal to zero, the conditions in if and else if are 'false' and the program execution falls to the statements in else clause block.
When you first entered this page, you received a greeting. This greeting is customized according to the time on the client (your) machine. Let's see how it works.
var d = new Date();
var t_hour = d.getHours();
if (t_hour <= 3)
{
alert("Hello dear visitor,nWorking late aren't we?");
}
else if (t_hour > 3 && t_hour < 12)
{
alert("Good morning dear visitor");
}
else if (t_hour >=12 && t_hour <= 16)
{
alert("Good afternoon dear visitor");
}
else
{
alert("Good Evening dear visitor");
}
We first initialize a variable using the new operator with the Date() contruct. The value of hours is stored in t_hour variable by employing the getHours() method of the date object. The value of t_hour variable is then passed through if-else if-else conditions. Thus, depending on the time of the day (time in hours), a greeting is displayed in an alert box.
You'll recall that JavaScript clock is a 24 hour clock.
- When t_hour is less than equal to 3 (0, 1, 2, or 3): alert box displays "Hello dear visitor,nWorking late aren't we?".
- When t_hour is more than 3 AND less than 12: alert box displays "Good morning dear visitor".
- If the value of t_hour is greater than equal to 12 AND less than equal to 16: "Good afternoon dear visitor" is displayed in the alert box.
- For all other values of t_hour (17, 18, 19, 20, 21, 22, and 23): alert box displays "Good Evening dear visitor".
You can check this script by changing the time on your machine and clicking on the above link.
The && and || operators
The && (AND) and || (OR) are logical operators. They are used to extend conditions.
if (number > 4)
{
statements...
}
The above condition checks is variable number is greater than 4. To check if this variable value lies between 10 and 4, we employ the && operator.
if (number > 4 && number < 10)
{
statements...
}
Thus, the statements inside the if block will be executed if number equals 5, 6, 7, 8 or 9.
Now, if we want to display an alert box if the value of number variable is less than 20 or more than 50, we use the || operator.
if (number < 20 || number > 50)
{
alert("...");
}
In the code above, an alert box will be displayed if number is 18, 5, -435, 51, 324... However, if the value of number lies between 20 and 50 (included 20 and 50), no alert box will be displayed.
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Categories
Accidents and Incidents
Art and Society
Business and Finance
Education and Employment
Environment and Development
Help and Aid
Science and Technology
Security and Politics
Share Market and Exchange Rates
Sports and Entertainment
Tourism and Foreign Affairs
Women and Health
Information
Women and Health
Food Recipes
Love and Relationship
Web and IT
AJAX Examples and Tutorials
Java Scripts Examples and Tutorials
PHP Scripts Examples and Tutorials
Men want good-looking women
Making sex exciting
Duminda unmasked by Anarkali
Too busy for Love Making?
Srikanth Vandana got a Baby Boy-Father and Son Born on Same Day!
Anjali’s days with Bharathiraja and K. Balachandar
Have you gone off sex?
Sex and Shoes
Dirty Talk!
Happy Birthday Surya
Alcohol makes men BETTER in the bedroom, scientists claim
Sexless Marriages
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Outsider Played Actress Sneha Hot Sexy Hip Cheap Photos?

































