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


JavaScript Programming

JavaScript | Programming | Decisions | Objects

JavaScript has a similar syntax to other programming languages. A JavaScript file is a series of commands. A command defines a statement. A statement generates an action. The  ;  semicolon declares the end of a statement. Multiple statements generated as a block are declared within  { }  curly brackets...

command statement;
command {statement, statement;}





A variable, also known as a scalar, is a command that stores a single value. Variable names are created and case-sensitive, but must initially be declared with the var command and only include letters, numbers, and underscores. A number is not allowed as the first character. The  $  dollar sign is allowed as the first character as an option. The lowercase or camelCap convention is recommended. A variable is assigned a value with the  =  equal sign as the assignment operator...

var variableName="This is a value.";

This is an alternative way to declare a variable...

var variableName;
variableName="This is a value.";


A string value is a series of letters, numbers, or symbols defined as text characters. String values are declared within  ' '  single quotes or  " "  double quotes. The  \  backslash is the escape operator for declaring quotes as string characters, as well as the  \  backslash itself.

A numeric value is an integer or decimal number, declared without quotes.

A boolean value is declared as either true or false, declared without quotes.

A null value is devoid of any value, declared without quotes.

An array value stores multiple text or numeric values.

An object value stores properties and methods. Tutorials for objects are on the Objects page.

var stringVariable="string value";
var numericVariable=36;
var booleanVariable=true;
var nullVariable=null;
var arrayVariable=["blue", "green", "yellow"];
var objectVariable=new ObjectName();

All JavaScript values evaluate true unless the value is the boolean false itself, the number 0, a string of length 0, null, or undefined.




JavaScript does not focus on input and output as in other programming languages. The  document.write()  function will be used to output values to the browser for these tutorials...

var variableName="This is a value.";

document.write(variableName);

The output is...

This is a value.



The  alert()  function outputs values to the browser with an alert box...

var variableName="This is a value.";

alert(variableName);

This is an example of an alert box.




An operator is a symbol that modifies values. The  +  concatenation operator connects strings and variables in sequence. This is an alternative method to quotes within quotes management. The concatenation operator is different than a plus sign used for arithmetic...

var variableName1="string value.";
var variableName2="This is a " + variableName1;

document.write(variableName2);

The output is...

This is a string value.



These are arithmetic operators...

add
subtract
multiply
divide
remainder
++  increment
--  decrement


These are comparison operators...

<=  less than or equal to
>=  greater than or equal to
!=  not equal to
==  equal to


These are logical operators...

&&  and
||  or
not




An array is like a variable, but can store multiple values in table format. An array has an index and value field. The index field, also known as the key, is the id field. The value field stores a value. By default, an array starts the index with a numeric value of 0, then continues in sequence. The index is declared within  [ ]  square brackets...

var colors=["blue", "green", "yellow"];

document.write(colors[1]);

This is an alternative way to declare an array...

var colors=[];
colors[0]="blue";
colors[1]="green";
colors[2]="yellow";

document.write(colors[1]);

An individual value from the array is called. The output is...

green



This is an alternative way to declare an array as a function, which turns a variable into an array...

var colors=new Array("blue", "green", "yellow");


An array can be defined with a string index, known as an associative array...

var colors=[];
colors["blue"]=0;
colors["green"]=1;
colors["yellow"]=2;


The value field of an array can also store other arrays, known as a multidimensional array...

var colors=["blue", "green", "yellow"];
var numbers=[1, 2, 3];

var multiArray=[colors, numbers];


A single value can be retrieved from a multidimensional array and stored in a new variable. The first number column [0] indicates the array being retrieved. The second number column [3] indicates the value being retrieved. The following example retrieves and stores the value "yellow" from the previous example...

var newVariable=multiArray[0][3];


The number of elements in an array can be counted and displayed with the length property...

var colors=["blue", "green", "yellow"];

document.write(colors.length);

The output is...

3




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