473,591 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to work with if/else statements in JavaScript

27 New Member
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 3881
hrstissiaopai
27 New Member
Can anyone confirm if this code actually works because a few times it didn't, for some reason.
Apr 17 '13 #2
omerbutt
638 Contributor
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
hrstissiaopai
27 New Member
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
97180
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 have written a theme in python for the superkaramba theme engine on kde (see http://netdragon.sourceforge.net - if you are a kde/linux user, it is a great visual applet engine). I have uploaded it to www.kdelook.org for others to download and...
6
2497
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 if statement by itself works just fine. Below is an example: if x >=0: DO SOMETHING Would it be better, or perhaps more complete, written like this:
3
3927
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 right direction about what I am not doing correctly? 1.. Write an If Then Else statement that displays the string "Pontiac" in the CarMakeLabel control if the CarTextBox control contains the string "Grand Am" (in any case).
1
2499
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 == value) { do this
5
4742
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 that it makes switch-case faster than if-else statements, is it true and if so what is the underlying concept behind a switch-case compilation? Can anyone help in this regard. Thanks in advance!!!
3
1761
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 many if else statements will I need for this?? Thanks!
0
2057
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
6510
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 vectors. or templates.
2
3858
by: avinash jain | last post by:
Could any one guide me how do we reduce nested if / else statements..
0
7934
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7870
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6639
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.