Saturday, August 20, 2011

LEARNING JAVASCRIPT - VARIABLE and TYPES - NUMBERS (JAVASCRIPT TUTORIAL)

  1. Welcome
    1. Introduction
    2. First Program
  2. Basics
    1. Placing the Code
    2. Lexical Structure
      1. Reserved Words
    3. Variables and Types
      Numbers StringsDatesArrays
    4. Operators
    5. Control Structures
    6. Functions and Objects
    7. Event Handling
    8. Regular Expressions
  3. Advanced Topics
    1. Object Oriented Programming
      1. Constructors and Prototypes
      2. Inheritance
      3. Access Control
    2. Functional Programming in JavaScript
      1. Function Objects
      2. Anonymous Functions
      3. Closures
      4. Higher Order Functions
    3. Modular JavaScript
      1. Modules
      2. Namespaces
    4. Optimization
    5. Metaprogramming
    6. Bookmarklets
  4. Debugging
    1. JavaScript Debuggers
    2. Common Mistakes
    3. Debugging Methods
    4. Browser Bugs
  5. Dynamic Web Client Programming (DHTML)
    1. Runtime Document Manipulation
      1. Introduction to the Document Object Model (DOM)
      2. Finding Elements
      3. Adding Elements
      4. Changing Elements
      5. Changing Element Styles
      6. Removing Elements
    2. Event Handlers
      1. Simple Event Handlers
      2. W3C Event Handlers
      3. Extended Event Handlers
    3. Running Scripts at Page Load
    4. Soul Building Javascript
    5. XML and Javascript
      1. DOM Manipulation
      2. XSL Sprinkles
    6. DHTML Examples
  6. Advanced Web Client Programming
    1. Working with Images
    2. Working with Forms
    3. Working With Cookies
    4. Client-Server Programming (AJAX)
      1. XMLHttpRequest
      2. Handling HTML
      3. Handling JSON
      4. Handling XML
    5. Design Mode
  7. JavaScript Outside HTML
    1. ActionScript in Flash
    2. Adobe PDF Forms
    3. JSOSA for the Macintosh
    4. JScript in Microsoft WSH
  8. Standards and Best Practices
    1. Naming Conventions
    2. Code Structuring
    3. Accessibility
  9. Appendices
    1. Index
    2. Links
    3. Useful Software Tools
    4. History of JavaScript
    5. Contributors


JavaScript implement numbers as floating point values, that is, they're attaining decimal values as well as whole number values. The numbers aren't objects, so they don't contain any methods that can be accessed by the normal dot notation. Instead a certain Math object provides usual number functions.

Contents

[hide]

[edit] Basic Use


To make a new number, a simple initialization suffices:

var foo = 0; // or whatever number you want

After you have made your number, you can then modify it as necessary. Numbers can be modified or assigned using the operators defined within JavaScript.

foo = 1; //foo = 1
foo += 2; //foo = 3 (the two gets added on)
foo -= 2; //foo = 1 (the two gets removed)

Number literals define the number value. In particular:

  • They appear as a set of digits of varying length.
  • Negative literal numbers have a minus sign before the set of digits.
  • Floating point literal numbers contain one decimal point, and may optionally use the E notation with the character e.
  • An integer literal may be prepended with "0", to indicate that a number is in base-8. (8 and 9 are not octal digits, and if found, cause the integer to be read in the normal base-10).
  • An integer literal may also be found with "0x", to indicate a hexadecimal number.

[edit] The Math Object


Unlike strings, arrays, and dates, the numbers aren't objects. The Math object provides numeric functions and constants as methods and properties. The methods and properties of the Math object are referenced using the dot operator in the usual way, for example:

var varOne = Math.ceil(8.5);
var varPi = Math.PI;
var sqrt3 = Math.sqrt(3);

[edit] Methods


[edit] random()


Generates a pseudo-random number.

var myInt = Math.random();

[edit] max(int1, int2)


Returns the highest number from the two numbers passed as arguments.

var myInt = Math.max(8, 9);
document.write(myInt); //9

[edit] min(int1, int2)


Returns the lowest number from the two numbers passed as arguments.

var myInt = Math.min(8, 9);
document.write(myInt); //8

[edit] floor(float)


Returns the greatest integer less than the number passed as an argument.

var myInt = Math.floor(90.8);
document.write(myInt); //90;

[edit] ceil(float)


Returns the least integer greater than the number passed as an argument.

var myInt = Math.ceil(90.8);
document.write(myInt); //91;

[edit] round(float)


Returns the closest integer to the number passed as an argument.

var myInt = Math.round(90.8);
document.write(myInt); //91;

[edit] Properties


Properties of the Math Object are most commonly used constants.

  • PI: Returns the value of pi.
  • E: Returns the constant e.
  • SQRT2: Returns the square root of 2.
  • LN10: Returns the natural logarithm of 10.
  • LN2: Returns the natural logarithm of 2.

No comments:

Post a Comment