Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


JavaScript Decisions

JavaScript | Programming | Decisions | Objects

Decisions are logical choices made based on the evaluation of variables and functions. Functions are self-contained blocks of code that can be reused. A function makes a decision depending on the evaluation of a condition. A condition is declared within  ( )  parenthesis. A function evaluates a condition then generates an action.

The  if()  function evaluates whether a condition is true or false. If the condition evaluates true, the statement is executed. If the condition evaluates false, the statement is not executed...

var num=6;

if(num < 12)
document.write("6 is less than 12.");

The condition is true. Using the  document.write()  function, the output is...

6 is less than 12.



This is an example using the  &&  and operator. Multiple statements are declared within  { }  curly brackets...

var num=6;

if(num < 12 && num > 3)
{alert("6 is less than 12...");
document.write("and 6 is greater than 3.");}

Both conditions are true so the statements are executed. The alert statement executes first. The output is...

6 is less than 12...

and 6 is greater than 3.



The else command generates alternative statements, one if the condition is true, or another if the condition is false. The  { }  curly brackets are not required for single statements, but are used for readability...

var num=18;

if(num < 12)
{document.write("18 is less than 12.");}
else
{document.write("18 is greater than 12.");}

The condition is false. The output is...

18 is greater than 12.



The  else if()  function adds another condition to the  if()  function...

var num=18;

if(num < 12)
{document.write("18 is less than 12.");}
else if(num < 24)
{document.write("18 is less than 24.");}
else
{document.write("Both conditions are false.");}

The output is...

18 is less than 24.




There is an alternative to evaluating a condition with ternary operators. The  ?  question mark is an operator that processes like the  if()  function and the  :  colon is an operator that processes like the else command...

var num=(age > 18) ? "yes" : "no";





There is a third alternative to evaluating a condition. The  switch()  function evaluates a variable as a condition. The case command compares the variable with a value. The  :  operator declares that the following statement is dependent on the condition. The break command stops the evaluation if the condition is true. The default command evaluates if all statements are false...

switch(action) {
case 'draw':
drawit();
break;
case 'eat':
eatit();
break;
default:
donothing();}





Loops are functions that repeat. The  while()  function is like the  if()  function, but the statement repeats when the condition evaluates true. When the condition evaluates false, the function stops looping. At the end of the statement block is a statement that changes the output of the condition...

var num=1;

while(num <= 10)
{document.write(num + " ");
num++;}

num++  changes the output of the condition. The output is...

1 2 3 4 5 6 7 8 9 10



The do command generates a statement before the  while()  function evaluates the condition. If the condition evaluates true, the statement repeats. If the condition evaluates false, the function stops...

var num=1;

do
{document.write(num + " ");
num = num + 1;}
while(num <= 10);

The output is...

1 2 3 4 5 6 7 8 9 10



The  for()  function is an alternative to the  while()  function...

for(num = 1;
num <= 10;
num++)
{document.write(num + " ");}

The output is...

1 2 3 4 5 6 7 8 9 10




User functions are created by the user and are made up of variables and functions. The function command declares a new user function, followed by the name of the function. User function names are created with the same naming conventions as a variable. Variables inside a user function have a local scope. Variables outside a user function have a global scope. A variable inside a function is global if declared without the var keyword. The statement of a user function is declared within  { }  side brackets. A user function is called by its name when needed...

function functionName() {
document.write('Hello universe!');}

functionName();

The output is...

Hello universe!



This is another example...

function functionName() {
var variableName1='Hello ';
var variableName2='universe!';
document.write(variableName1 + variableName2);}

functionName();

The output is...

Hello universe!



An argument can be passed when calling the function. An argument is a value passed to the function and stored in a variable local to the function. The argument variable is declared within  ( )  parenthesis...

function functionName(argumentName) {
document.write(argumentName);}

functionName('This value is passed as an argument to the function.');

The output is...

This value is passed as an argument to the function.



Multiple arguments can be passed and are separated by commas...

function functionName(argument1, argument2) {
document.write(argument1 + '<br />' + argument2);}

functionName('value1', 'value2');

The output is...

value1
value2




This is another example...

function numberFunction(num1, num2) {
var total=num1 + num2;
document.write(total);}

numberFunction(12, 12);

The output is...

72



Arguments passed to a function are automatically stored in an array called arguments[], available only within the function. Argument names in functions do not need to be declared when passing arguments because the arguments are automatically available in the arguments[] array, which can receive an unlimited number of arguments.



A user function can return a value. The return command calls up the value...

function myPlanet() {
return ("Hello universe!");}

document.write(myPlanet())

The output is...

Hello universe!



This is another example...

function functionName(num) {
return num * num;}

variableName=functionName(3);

document.write(variableName);

The output is...

9




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington