Archive for August, 2007

JavaScript Data Types

Posted in JavaScript on August 7, 2007 by Joey

JavaScript supports the following data types::

Number: JavaScript supports both integer and floating-point numbers. Examples are 3 and 3.33.

String: Examples are “Joey JavaScript” and “Today is Monday.”

Boolean: Represents true and false values.

null: Has no value. It means the variable holds nothing.

undefined: Used to check for the existence of a variable or whether the variable has been assigned a value yet.

Commenting JavaScript Code

Posted in JavaScript on August 7, 2007 by Joey

There are two ways to comment JavaScript code. If only one line of code needs to be commented, use two consecutive forward slashes:

//This is a JavaScript single-line code comment

To comment a block of code, do the following:

/*
This is a JavaScript
code block comment
*/

JavaScript Concatenation Operation: The Plus Sign

Posted in JavaScript, programming on August 4, 2007 by Joey

The JavaScript concatenation operator is the plus sign. As shown in the example below, the plus sign in JavaScript can be used to concatenate strings:

var strA = 'Joey';
var strB = 'JavaScript';
var strC = strA + ' ' + strB;
document.write(strC);//Result is Joey JavaScript

Strings can also be concatenated with the shorthand assignment operator +=, as follows:

var strA = 'Joey';
var strB = ' JavaScript';
var strC = strA += strB;
document.write(strC);//Result is Joey JavaScript

A number concatenated with a string, results in a string, as follows:

var strA = 'Joey';
var strB = 5;
var strC = strA += strB;
document.write(strC); //Result is Joey5

If two numbers are concatenated together, JavaScript is no longer concatenating, as this is now an addition operation:

var strA = 5;
var strB = 5;
var strC = strA += strB;
document.write(strC);//Result is 10

Had one or both of the numbers been in quotes, however, JavaScript will treat this as a string operation:

var strA = '5';
var strB = 5;
var strC = strA += strB;
document.write(strC);//Result is 55

How to Get the Length of a String in JavaScript

Posted in JavaScript, programming on August 4, 2007 by Joey

To retrieve the length of a JavaScript string, access the length property of the JavaScript string object, as follows:

var strTest = 'Joey JavaScript';
document.write(strTest.length);

Note that an empty string has a length of 0.

JavaScript True, False: Checking for Boolean Values

Posted in JavaScript, programming on August 4, 2007 by Joey

Checking if a value is true or false is simple in JavaScript. All values evaluate to true, except for:

0
-0
null
undefined
NaN
empty string
false

For example, the following results in false being output since the empty string evaluates to false.

var testVar = ''
if (testVar)
document.write('true');
else
document.write('false');

It should also be noted that the literal strings '0' and 'false' evaluate to true.

JavaScript String Object lastIndexOf Method

Posted in JavaScript, programming on August 3, 2007 by Joey

The JavaScript string object contains an lastIndexOf method. It returns the index in the string of the last occurrence of a specified character. The index is numbered starting at 0. For example:

var strTest = 'joey javascript';
document.write(strTest.lastIndexOf('t'));

The index returned in the preceding example is 14, as there are 15 characters in the string, “t” is the last character and the index is numbered starting at 0.

The lastIndexOf method accepts an optional second argument – the position at which to begin the search.

var strTest = 'joey javascript';
document.write(strTest.lastIndexOf('j'));

In this example the index returned is 5. Since the search starts at the end of the string and works backwards it finds the last “j”, which is at position 5 in the string. If we tell the string to start looking backwards from the second position, however, the value returned will be the “j” at position 0, as follows:

var strTest = 'joey javascript';
document.write(strTest.lastIndexOf('j',2));

Related articles:
JavaScript String Object indexOf method

JavaScript Regular Expressions: Using Parentheses to Constrain Alternation

Posted in JavaScript, Regular Expressions on August 3, 2007 by Joey

In JavaScript regular expressions, parentheses can be used to constrain alternation. For example, if a string needs to be matched that begins with “hello” or “hi”, followed by a dash, one might try this regular expression:

document.write(/^hello|hi-/.test('hello'));//returns true

This will match a string that starts with “hello” or “hi” but will only require that the “hi” be followed by a dash. In order to constrain both “hello” and “hi” followed by a dash, parentheses need to be used:

document.write(/^(hello|hi)-/.test('hello'));//returns false

Related articles:
JavaScript Regular Expressions
JavaScript Regular Expression OR

Viewing a Web Site’s JavaScript with Firebug

Posted in JavaScript, Software, Web, Web 2.0, internet, programming on August 2, 2007 by Joey

The JavaScript for a web site is accessible through the Firebug extension for Firefox. When viewing a web page, open Firebug in the Firefox browser by clicking “Tools” – “Firebug” – “Open Firebug”. (Note: The Firebug Extension must be installed first.) Next, click on the “Script” tab on the Firebug menu bar. Above the menu bar, a dropdown will appear. Click on the dropdown and a listing of the JavaScript files included in the page will appear. Select the file you want to view and the JavaScript for that file will appear in the Firebug code viewing pane. This is illustrated in the example below.

firebug

Related articles:
Debugging JavaScript with the Firebug Extension for Firefox

Debugging JavaScript with the Firebug Extension for Firefox

Posted in JavaScript, Software, programming on August 1, 2007 by Joey

The Firebug extension for Firefox can be used to debug JavaScript, as well as CSS, HTML and DOM.

To start using Firebug, download the extension here. Once the extension is installed, it can be accessed from the Firefox menu bar, under “Tools” – “Firebug.”

To use Firebug to debug JavaScript, I recommend referencing Firebug’s JavaScript documentation, which is located here.

Related articles:
Viewing a Web Site’s JavaScript with Firebug