IT 4 BLONDES

Beautiful women doing geeky stuff

JavaScript cheat sheet

Related Posts

28/09/2013 Categories: JavaScript Tools

From JavaScript for Dummies

  • 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.

  • alert("message")

Creates a popup dialog containing "message."

  • stringVar = prompt("message")

Send message to user in a dialog box, retrieve text input from user and store it in stringVar.

 

Operators

  • Equality == (x==3) - Works with all variable types, including strings.
  • Not equal != (x != 3) - True if values are not equal.
  • Less than < (x < 3) - Numeric or alphabetical comparison.
  • Greater than > (x > 3) - Numeric or alphabetical comparison.
  • Less than or equal to <= (x <= 3) - Numeric or alphabetical comparison.
  • Greater than or equal to >= (x >= 3) - Numeric or alphabetical comparison.