Code to Use in JavaScript Variable Manipulation Functions

By Andy Harris

Part of JavaScript & AJAX For Dummies Cheat Sheet

As shown in the following table, you can use these JavaScript statements in your own code to create and modify variables in your JavaScript functions.

Element Description
var myVar = 0; Creates a variable with given starting value. Type is determined dynamically.
stringVar = prompt("message") Sends message to user in a dialog box, retrieves text input from user and stores it in stringVar.
stringVar.length Returns the length (in characters) of stringVar.
stringVar.toUpperCase(), stringVar.toLowerCase() Converts stringVar to upper- or lowercase.
stringVar.substring() Returns a specified subset of stringVar.
stringVar.indexOf() Returns location of a substring in stringVar (or -1).
parseInt() Converts string to int.
parseFloat() Converts string to float.
toString() Converts any variable to string.
eval() Evaluates string as JavaScript code.
Math.ceil() Converts any number to integer by rounding up.
Math.floor() Converts any number to integer by rounding down.
Math.round() Converts any number to integer by standard rounding algorithm.
Math.random() Returns random float between 0 and 1.