473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

How to work with if/else statements in JavaScript

One very important part of almost all computer languages are conditional statements, and specifically if/else statements. Here is an example.
Expand|Select|Wrap|Line Numbers
  1. if(16==16){
  2. window.alert("Yes, 16 does equal 16! Amazing!");
  3. }
  4. else {
  5. window.alert("Wait a second... 16 is not equal to 16?");
  6. }
  7.  
Here is an explanation. This code states that if 16 is equal to 16,
"Yes, 16 does equal 16! Amazing!" will show up in an alert box. If it does not, "Wait a second... 16 is not equal to 16?" will show up in an alert box. Notice that in the parentheses next to the if the two numbers are compared using two equal signs and not just one:
Expand|Select|Wrap|Line Numbers
  1. if(16==16)
  2.  
The reason for this is that = is an assignment operator, which is used to assign values to variables, but == is a comparison operator, used to compare two values. Other comparison operators include <= (less than or equal to), >= ( greater than or equal to), =, > and <.
The code shown above, however, is not really useful. A way of making it useful is shown below:
Expand|Select|Wrap|Line Numbers
  1. var age=prompt("What is your age?");
  2. if(age<6){
  3. window.alert("You are under 6. You need the parental password.");
  4. var pass=prompt("Type in the password to continue.");
  5. if(pass=="parentpassword"){
  6. window.alert("The password you inserted is correct. Go on!");
  7. window.location="google.com";
  8. }
  9. else{
  10. window.alert("Your password is wrong");
  11. }
  12. }
  13. else{
  14. window.alert("You are over 6. You are good to go.");
  15. window.location="google.com";
  16. }
  17.  
As you can see in the above code, if/else statements are very helpful, and you can also put if/else statements inside other if/else statements (also called nested if/else statements). They form a fundamental part of many computer programs. Try experimenting with them!
Mar 16 '13 #1
3 3868
Can anyone confirm if this code actually works because a few times it didn't, for some reason.
Apr 17 '13 #2
omerbutt
638 512MB
yes it does work for me in FF and IE 10 , which browser were you using to test the code.
regards,
Omer Aslam
Apr 25 '13 #3
Thanks. I usually use Chrome or Opera, but I also have IE and FF. I appreciate you taking the time to test it.
P.S.: Isn't IE 10 Windows 8-only?
Apr 25 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
1
by: Doo0592 | last post by:
Hi all, Judging by the topics in here this should be a snap for all of you! Can any one explain why when i use the window.confirm in an if else statement it behaves like this: if (var ==...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
3
by: floofy | last post by:
Hi! I need to create a program that asks for three numbers from the user, and then using if else statements, I have to get the program to output the two largest numbers. I only need a hint, how...
0
by: GGawaran | last post by:
Did a search but couldn't find anything to answer my question. In asp, i am using an if else statement for a variable. For Example; //Declared variable// dim greg
21
by: avinash jain | last post by:
Hi Could any one help me ... I need to avoid If / else statements in my code. I have searched but I could not any simple solution for that. Its said to use vectors.But I ddont want to use those...
2
by: avinash jain | last post by:
Could any one guide me how do we reduce nested if / else statements..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.