One of the strengths of JScript is that a lot of the difficult work has been done for you, with methods and functions covering a wide range of functionality. From performing calculations to manipulating dates and strings, JScript has a method or function to suit you.
Since JScript variables can be of many different types, there are a couple of conversion functions you will be using often if you want to ensure that a particular field value is either an integer or floating decimal point number. These conversion functions are listed in Table B-4.
Operator | Description | Example Usage |
---|---|---|
parseInt | For parsing other variable types to integer | parseInt(Quantity); |
parseFloat | For parsing other variable types to a floating decimal point number | parseFloat(OrderTotal); |
String functions within JScript treat strings like an array and can be used to find substrings within a string, change the case of a string, concatenate two strings, and more. Table B-5 lists and describes the string functions.
Function | Description | Usage |
---|---|---|
charAt | Used to return a character at a certain position within a string. | String.charAt(position); |
indexOf | Used to return the position at which a substring exists within a string, searching from left to right. Can also specify a second argument as a starting point. | String.indexOf(substring); |
lastIndexOf | Used to return the position at which a substring exists within a string, searching from right to left. Can also specify a second argument as a starting point. | String.indexOf(substring); MyString.indexOf(“Market”); Returns 2 |
split | Can be used to split a string into substrings using a delimiter (spaces, and so on) and put the resulting substrings into an array. | String.split(“ “); |
toLowerCase | Used to convert a string to lowercase. | String.toLowerCase(); |
toUpperCase | User to convert a string to uppercase. | String.toUpperCase(); |
length | Used to determine the length of a string. | String.length; |
The date and time functions within JScript are easy to use and provide methods to pull apart a date and work with the components. All the examples in Table B-6, which lists the date and time functions, assume that the variable InvoiceDate is a date-time field with a value of 12/11/2003 8:10:57 AM.
Function | Description | Example Usage |
---|---|---|
getDate | Returns the day-of-month value (1 to 31) from a date field | InvoiceDate.getDate() Returns 11 |
getDay | Returns the day of the week, where: | InvoiceDate.getDay() Returns 4 |
getFullYear | Returns the full year from a date field | InvoiceDate.getFullYear() Returns 2003 |
getHours | Returns the hours value from a date field | InvoiceDate.getHours() Returns 8 |
getMinutes | Returns the minutes value from a date field | InvoiceDate.getMinutes() Returns 10 |
getMonth | Returns the month value from a date field, where: | InvoiceDate.getMonth() Returns 11 |
getSeconds | Returns the seconds value from a date field | InvoiceDate.getSeconds() Returns 57 |
getTime | Returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object | InvoiceDate.getTime() Returns 1071090659513 |
getYear | Returns the year value from a date field | InvoiceDate.getYear() Returns 2003 |
Arrays can easily be created in JScript by a simple declaration. And then any array elements can be referenced using square brackets []. Arrays in JScript are zero-based, so the first element will always be 0, as shown in the following example:
var myArray = new Array();
for (i = 0; i < 10; i++)
{
myArray[i] = i;
}