Ad

Ad

Showing posts with label Statements. Show all posts
Showing posts with label Statements. Show all posts

Sunday, 4 May 2014

JavaScript If...Else Statements

Conditional statements are used to perform different actions based on different conditions.

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
  • if statement - use this statement to execute some code only if a specified condition is true
  • if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
  • if...else if....else statement - use this statement to select one of many blocks of code to be executed
  • switch statement - use this statement to select one of many blocks of code to be executed

If Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition)
  {
  code to be executed if condition is true
  }
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!

Example

Make a "Good day" greeting if the time is less than 20:00:
if (time<20)
  {
  x="Good day";
  }
The result of x will be:
Good day

Try it Yourself »
Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true.

If...else Statement

Use the if....else statement to execute some code if a condition is true and another code if the condition is not true.

Syntax

if (condition)
  {
  code to be executed if condition is true
  }
else
  {
  code to be executed if condition is not true
  }

Example

If the time is less than 20:00, you will get a "Good day" greeting, otherwise you will get a "Good evening" greeting
if (time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }
The result of x will be:
Good day

Try it Yourself »


If...else if...else Statement

Use the if....else if...else statement to select one of several blocks of code to be executed.

Syntax

if (condition1)
  {
  code to be executed if condition1 is true
  }
else if (condition2)
  {
  code to be executed if condition2 is true
  }
else
  {
  code to be executed if neither condition1 nor condition2 is true
  }

Example

If the time is less than 10:00, you will get a "Good morning" greeting, if not, but the time is less than 20:00, you will get a "Good day" greeting, otherwise you will get a "Good evening" greeting:
if (time<10)
  {
  x="Good morning";
  }
else if (time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }
The result of x will be:
Good day

Try it Yourself »

Saturday, 3 May 2014

JavaScript Statements

In HTML, JavaScript statements are command lines executed by the web browser.

JavaScript Statements

In HTML, JavaScript statements are "commands" to the browser.
The purpose, of the statements, is to tell the browser what to do.
This JavaScript statement tells the browser to write "Hello Dolly" inside an HTML element identified with id="demo":

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";

Try it Yourself »


Semicolon ;

Semicolon separates JavaScript statements.
Normally you add a semicolon at the end of each executable statement.
Using semicolons also makes it possible to write many statements on one line.
Writing:

a = 5;
b = 6;
c = a + b;
Is the same as writing:
a = 5; b = 6; c = a + b;

Try it Yourself »

NoteYou might see examples without semicolons.
Ending statements with semicolon is optional in JavaScript.


JavaScript Code

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
Each statement is executed by the browser in the sequence they are written.
This example will manipulate two different HTML elements:

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";
document.getElementById("myDiv").innerHTML = "How are you?";

Try it Yourself »


JavaScript Code Blocks

JavaScript statements can be grouped together in blocks.
Blocks start with a left curly bracket, and end with a right curly bracket.
The purpose of a block is to make the sequence of statements execute together.
A good example of statements grouped together in blocks, are in JavaScript functions.
This example will run a function that will manipulate two HTML elements:

Example

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello Dolly.";
    document.getElementById("myDIV").innerHTML = "How are you?";
}

Try it Yourself »
You will learn much more about functions later in this tutorial.

JavaScript Statement Identifiers

JavaScript statements often start with a statement identifier to identify the JavaScript action to be performed.
Statement identifiers are reserved words and cannot be used as variable names (or any other things).
Here is a list of some of the JavaScript statements (reserved words) you will learn about in this tutorial:
StatementDescription
breakTerminates a switch or a loop.  
catchMarks the block of statements to be executed when an error occurs in a try block.
continueJumps out of a loop and starts at the top.
do ... whileExecutes a block of statements and repeats the block while a condition is true.
forMarks a block of statements to be executed as long as a condition is true.
for ... inMarks a block of statements to be executed for each element of an object (or array).
functionDeclares a function.
if ... elseMarks a block of statements to be executed depending on a condition.
returnExits a function.
switchMarks a block of statements to be executed depending on different cases.
throwThrows (generates) an error.
tryImplements error handling to a block of statements.
varDeclares a variable.
whileMarks a block of statements to be executed while a condition is true.
There is a complete list of reserved words, in a later chapter of this tutorial.

White Space in JavaScript Statements

JavaScript ignores extra spaces. You can add white space to your script to make it more readable.
The following lines are equivalent:
var person="Hege"
var person = "Hege"


Breaking a JavaScript Statement

You can break up a code line within a text string with a backslash:
var text = "Hello \
World!";
However, you cannot break up a code line like this:
var text = \ "Hello World!";