Introduction to JavaScript Programming – part 2

Introduction to JavaScript Programming – part 2: Continuing from Introduction to JavaScript Programming – part 1.

In previous part I just gave an introduction to JavaScript Programming, and also I explained what is a variable in JavaScript. In this part we are going to discuss about JavaScript Strings and JavaScript Arrays.

JavaScript Strings

If you need to concatenate two or more strings, use the + operator.
example

strFirst = "John";
 strLast = "Kennedy";
 strFull = strFirst + "F." strLast;
  • If you place a quotation mark within a string, it may cause an error. To use a quotation mark within a string, use the escape code “.

example

strQuote = "don "t forget the escape code";

Escape Codes Used by JavaScript

' single quotation mark
 " double quotation mark
 back slash
 \r carriage return
 \n new line
 \t tab
JavaScript Arrays

An array is list or table of variables of the same data type. For example to create an array containing the names of colors you might use the code below.

strColor = new Array(6);
 strColor(0) = "black";
 strColor(1) = "blue";
 strColor(2) = "green";
 strColor(3) = "orange";
 strColor(4) = "red";
 strColor(5) = "white";

Access an element of an array by using its index. For example to access the array element “orange”, use the code below.

strFavorite = strColor(3);

Notes:
– The first element in the array always has an index of 0. The last element in an array has an index one less than the size used to declare the array.
– You can declare and initialize an array at the same time as shown below.

color = new Array("black", "blue", "green", "orange", "red", "white");

If you don’t know how many elements will be in the array, you can declare it without a size as shown below.

strMembers = new Array();

This note will continue in Introduction to JavaScript Programming – part 3 page

About

Find me on Google+, Twitter and Facebook.
%d bloggers like this: