Understanding JavaScript commands

When using JavaScript, you need to employ commands to make things happen. Commands are sometimes referred to as functions, statements or methods.

Keep in mind that JavaScript has a set of these commands or methods, and jQuery has is own set of methods. The JavaScript methods will work on any browser without having to link to jQuery library files, but the jQuery methods require that the proper jQuery library files are loaded into the browser with the web page.

You can also create you own commands for frequently used tasks done in your script with both JavaScript and jQuery.


Commands have parentheses

In both JavaScript and jQuery, commands have a set of parentheses.

In many cases something will need to be put inside the parentheses to tell the browser how to deploy the command. Some commands do not require data inside of the parentheses, like .pop() or .shift().

If the contents of the parentheses is text (a string value) to be added to the page, or to an array, or whatever, it needs to be put inside of a pair of single or double quotes (and they must be a matching pair of quotes '...' or "...").

If a variable name, number or Boolean value is to be used, do not enclose the value in parentheses.

alert('You have won!');
// this creates a popup box displaying the contents between the quotes

//often you may need to add HTML tags to the contents

guestList.push("Paul Burkhardt");  
// the .push() command puts the contents at the end of the array named guestList

// if adding a number, no quotation marks

// if using a variable name, no quotation marks

// if using a Boolean value, no quotation marks


Commands may start with a period

When you see commands shown in the list or referred to in the text, you may or may not see a period in front.

The commands without periods can stand alone in a statement, like alert() or isNaN().

The commands with a period in front require additional information to precede them, like .pop() or .shift() require the name of the array to precede the command.


JavaScript: common commands

alert() displays the contents between the parentheses in a popup dialog box
document.write() writes the contents between the parentheses to the web page
.indexOf() returns the 1st index at which the specified element is found in an array
isNaN() returns true if the value being tested is NOT a number 
location.reload(); refreshes the document when placed in a function
.match() returns matches to a regular expression in a string
parseInt() will turn the value of a string variable into an integer
.pop() removes the last item from an array
.preventDefault(); will cancel the default event
prompt() creates a popup dialog box with a text box that returns what was typed into the text box for use in the program
.push() places the contents of the parentheses at the end of an array
.replace() searches for a match to a regular expresion and replace it with another string
return value return is often placed inside a function and returns the value stated
.search() returns the position of a matched string from another string
.shift() removes the first item from an array 
.splice()  adds or removes items from an array at a specified index number and for a specified number of items. 
.test() checks for a match between a regular expression and a specified string
.toLowerCase() returns the value in all lower case letters
.toUpperCase() returns the value in all upper case letters